C# IStructuralEquatable Kullanımı Üzerinde Buzz söylenti

If equality is derece needed for the derived class you birey skip IEquatable but you need to override the CanEqual to prevent it being equal with base classes (unless of course they should be considered equal).

1 How do such comparators relate to things like Dictionary and other collections? I know that Dictionary seems to handle structures sensibly albeit slowly in .

Other types which implement structural equality/comparability include tuples and anonymous types - which both clearly benefit from the ability to perform comparison based on their structure and content. A question you didn't ask is:

Equals and object.ReferenceEquals. Equals is meant to be overridden for whatever sort of comparison makes the most sense for a given type, whereas ReferenceEquals yaşama't be overridden and always compares by reference.

I'm amazed that the most important reason is not mentioned here. IEquatable was introduced mainly for structs for two reasons:

The generic tuple classes (Tuple, Tuple, Tuple, and so on) and the Array class provide explicit implementations of the IStructuralEquatable interface. By casting (in C#) or converting (in Visual Basic) the current instance of an array or tuple to an IStructuralEquatable interface value and providing your IEqualityComparer implementation kakım an argument C# IStructuralEquatable nedir to the Equals method, you kişi define a custom equality comparison for the array or collection.

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.

This is very disappointing behaviour from Microsoft; I'm now wondering if I should review the list of cases I've filed and see if other ones I've submitted have been removed...

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

In addition to this awesome blog Frank and I also dicussed all of this awesome in detail on Merge Conflict on episode 111:

Amma velakin bu yapımız class kadar kompleks maslahatlemler yürekin tasarlanmış bir yapı gerektirmiyorsa ve tutulacak verileri enkapsüle kılmak yetiyorsa ahacık bu konsept durumlarda struct yapısını yeğleme edebiliriz.

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

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 objects.

Being able to specify IStructuralEquatable/IStructuralComparable in such cases is actually useful. It would also be inconvenient to pass a TupleComparer or ArrayComparer everywhere you want to apply this type of comparison. The two approaches are hamiş mutually exclusive.

Leave a Reply

Your email address will not be published. Required fields are marked *