Detaylar, Kurgu ve C# IStructuralEquatable Temel Özellikleri

Why do we have IStructuralComparable and IStructuralEquatable when there already exist the IComparable and IEquatable interfaces? The answer I would offer is that, in general, it's desirable to differentiate between reference comparisons and structural comparisons.

In certain scenarios (such as using the value type kakım a key in a dictionary) it can murder performance in one foul swoop.

If you want to implement IEquatable in a class hierarchy you sevimli use the following pattern. It prevents derived (including sibling) classes from being equal.

Daniel A.A. PelsmaekerDaniel A.A. Pelsmaeker 49.2k2121 gold badges112112 silver badges160160 bronze badges 5 In addition to answers which point to duplicate hashcodes bey is documented behavior, some reasoning and reflection would also lead you to the same conclusion.

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

comparer IEqualityComparer An object that determines whether the current instance and other are equal.

What does IEquatable buy you, exactly? The only reason I can see it being useful is when creating a generic type and forcing users to implement and write a good equals method.

In my implementation I delegated the task of calculating hash codes to the internal array. While testing it, to my great surprise, I found that my two different arrays had the same structural hash code

comparer IEqualityComparer İki nesnenin hemayar olup olmadığını kullanmak sinein kullanılacak yöntemi tanımlayan nesne.

I had the same question. When I ran LBushkin's example I was surprised to see that I got a different answer! Even though that answer katışıksız 8 upvotes, it is C# IStructuralEquatable Temel Özellikleri wrong. After a lot of 'reflector'ing, here is my take on things.

Amma velakin bu imalız class derece kompleks kârlemler karınin tasarlanmış bir yapı gerektirmiyorsa ve tutulacak verileri enkapsüle buyurmak yetiyorsa ahacık bu yol 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.

Specifically, I do derece know the exact type of the object. The only assumption I make is that it inherit from IStructuralEquatable.

Leave a Reply

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