Since immutable data strucutures are first-class values we can compare them for equality or order as we do with any other values. But things became complicated in BCL immutable collections preview because every immutable collection can be parameterized by IEqualityComparer<T>
/IComparer<T>
instances. Looks like immutable collections with different comparers should not be allowed to compare (since equality is not defined for comparers itself), because it makes equality relation non-symmetric:
var xs = ImmutableList<string>.Empty.Add("AAA")
.WithComparer(StringComparer.OrdinalIgnoreCase);
var ys = ImmutableList<string>.Empty.Add("aaa")
.WithComparer(StringComparer.Ordinal);
Console.WriteLine(xs.Equals(ys)); // true
Console.WriteLine(ys.Equals(xs)); // false
Will this behavior be fixed somehow?
Equality is a difficult thing to define, and even more difficult to achieve consensus on that definition in a room of smart engineers. :)
We're actually going to be removing value-equality from the Equals
and GetHashCode
methods of the immutable collections so that these methods provide the same speed (and near uselessness) as most other types in the BCL and in customer code.
We do very much hope to add value-equality methods to the immutable collections in a future release.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With