Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 ) ?

@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 <T> ....? Royi Namir Mar 3, 2012 at 17:43 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 3, 2012 at 17:44 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 3, 2012 at 17:46 @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 referencesource.microsoft.com/#mscorlib/system/collections/… , then you will see how it work. user9623401 Mar 3, 2021 at 14:23

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.

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.

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<T>.Equals(T other) . But why would you ever need IStructuralEquatable ? user541686 Mar 4, 2012 at 0:39

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .