21

according to msdn

IStructuralEquatable

Defines methods to support the comparison of objects for structural equality. Structural equality means that two objects are equal because they have equal values. It differs from reference equality, which indicates that two object references are equal because they reference the same physical object.

isnt it what Equals should do ? ( when overriding IEquatable) ?

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • @CodeInChaos From my reading - its only for composite types ( which contains some elements)....right ? - otherwise - if it was just one object - i could have implement equals ....? – Royi Namir Mar 03 '12 at 17:43
  • 1
    My understanding is that it's used for collection like types, and encapsulates the structural part of the comparison, but leaved the comparison of the elements to a comparer passed in by the user. But I'm not really sure if I really got it. – CodesInChaos Mar 03 '12 at 17:44
  • http://stackoverflow.com/a/5601068/445517 But I think the accepted answer is still(he claimed to have corrected it) wrong/not getting the point of `IStructuralEquatable`. – CodesInChaos Mar 03 '12 at 17:46
  • @CodeInChaos can you please rewrite your answer as an answer so i can check it – Royi Namir Mar 03 '12 at 18:04
  • @RoyiNamir user844541's answer is correct, but maybe it is still hard for you to understand without a concrete example, if you are familiar with `IEqualityComparer` and how it is used by Linq's Distinct(), then after check the source code to see how it implement `IStructuralEquatable` on https://referencesource.microsoft.com/#mscorlib/system/collections/iequalitycomparer.cs,6be91fab5c61f2d0, then you will see how it work. –  Mar 03 '21 at 14:23
  • The short answer is, no, you cannot override `IEquatable` because Array is an abstract class and the compiler does some extra work internally(check the CLR via C# book Chapter 16, so Array itself has no idea about how to compare its elements where those elements could be any type, so you really need an external `IEqualityComparer ` passed into Array for it to compare its elements. That's why IStructuralEquatable's Equals method takes `IEqualityComparer` as a parameter –  Mar 03 '21 at 14:27

2 Answers2

2

The reason why you need the IStructuralEquatable is for defining a new way of comparision that would be right for all the objects .

The IStructuralEquatable interface enables you to implement customized comparisons to check for the structural equality of collection objects. That is, you can create your own definition of structural equality and specify that this definition be used with a collection type that accepts the IStructuralEquatable interface.

For example if you want a list that will sort all its elements by a specific definition. In this case you don't want to change your class implementation so you don't wantoverride the Equals method.

this will define a general way to compare objects in your application.

user844541
  • 2,868
  • 5
  • 32
  • 60
0

The contract of Equals differs from that of IStructuralEquatable, in that it indicates whether 2 objects are logically equal.

By default, Equals on a reference type indicates whether two object references reference the same object instance. However, you are able to override Equals according to the logic of your application.

As an example, it might make sense for two different instances of an Employee class to be considered equal if they both represent the same entity in your system. To achieve this, employee objects with matching SSN properties would be treated as logically equal, even if they were not structurally equal.

Dan Malcolm
  • 4,382
  • 2
  • 33
  • 27
  • 4
    Er... if you want *logical* equivalence, you override `Object.Equals(Object other)`. If you want *referential* equivalence, you simply don't override it. If you have a `struct`, and you want a *logical* comparison (which you almost always do), you override that method, and you also implement `IEquatable.Equals(T other)`. But why would you ever need `IStructuralEquatable`? – user541686 Mar 04 '12 at 00:39