System.Collections.IStructuralEquatable
and System.Collections.IStructuralComparable
were added in .NET 4, but why aren't they generic, like IEquatable<T>
and IComparable<T>
?
Asked
Active
Viewed 1,475 times
9

thecoop
- 45,220
- 19
- 132
- 189
-
Because C# generics don't let you specify structural requirements for a type parameter. – Ani Feb 11 '11 at 12:27
1 Answers
3
The example on MSDN gives part of the answer here; it seems to be useful for heterogeneous equality, rather than homogeneous equality - i.e. for testing whether two objects (/values) of potentially different types should be considered equal. In such scenarios, it is extremely likely that the calling code is dealing with object
(to represent heterogeneous data). And generic methods don't play nicely then.

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
-
2However, all the `Tuple` classes simply return `false` if the other object isn't exactly the same type. `Array` seems to implement it correctly... – thecoop Feb 11 '11 at 12:08
-
Array behaves the same as tuple for me. This test fails: Assert.IsTrue(StructuralComparisons.StructuralEqualityComparer.Equals(new[] { 5, 10 }, new[] { 5.0, 10.0 })); – Hrvoje Prgeša Dec 03 '15 at 12:36
-
There is no need for an equality operator that accepts different types. That should not even compile. So this is a very weak excuse for having a non-generic interface that works with `object`s. – Charles Roddie May 12 '20 at 21:12