record types with collection properties & collections with value semantics

It looks like there is currently no such type available.

You can implement this yourself, but beware of the implications if you need this in a production-ready way. It’s not so simple as it may seem at first sight, as this answer by @ryanholden8 demonstrates!

For my use case (and as a simplified example) I went along with this gist which decorates an IImutableList and can be used as follows:

var r1 = new SomeRecord(0, "test", new[] { 1, 2 }.ToImmutableList().WithValueSemantics());
var r2 = new SomeRecord(0, "test", new[] { 1, 2 }.ToImmutableList().WithValueSemantics());
Console.WriteLine(r1 == r2); // true

Obviously beware of the performance implications for very large lists.

Leave a Comment