System.Collections.Immutable types: why no .Equals

Viewed 1413
var a = ImmutableList<int>.Empty.Add(1).Add(2).Add(3);
var b = ImmutableList<int>.Empty.Add(1).Add(2).Add(3);

Console.WriteLine(a.Equals(b)); // False

In the code above the a.Equals(b) calls Object.Equals, because ImmutableList<T> doens't override Equals(object), and as ImmutableList<T> is a reference type Object.Equals does (the useless) reference comparison.

Question: Why doesn't ImmutableList<T> override .Equals? It would be straightforward and expected to have it compare each contained object with .Equals and return the result based on those comparisons. It would even be consistent to test rest of the framework (see class String )

note: the above code is tested with System.Collections.Immutable.1.1.38-beta-23516

3 Answers

You 'could' create a wrapper class and override the Equality and HashCode methods... This could be helpful for c# records so that you don't have to keep overriding the auto generated methods.

    public sealed class ImmutableListSequence<T>
    {
        public ImmutableListSequence(ImmutableList<T> items)
        {
            Items = items;
        }

        public ImmutableList<T> Items { get; }

        public override int GetHashCode()
        {
            unchecked
            {
                return Items.Aggregate(0, (agg, curr) => (agg * 397) ^ (curr != null ? curr.GetHashCode() : 0));
            }
        }

        public override bool Equals(object? obj)
        {
            if (obj is ImmutableListSequence<T> second)
            {
                return Items.SequenceEqual(second.Items);
            }

            return false;
        }
    }
Related