Hakkında C# IStructuralEquatable Temel Özellikleri

It's normally expected that if you implement IEquatable.Equals you will also override Object.Equals to be consistent. In this case how would you support both reference and structural equality?

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

. The best example of this is arrays, which with .NET 4 now implement the IStructuralEquatable interface. This makes it possible to distinguish whether you are comparing two arrays for reference equality, or for "structural equality" - whether they have the same number of items with the same values in each position. Here's an example:

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 sevimli't be overridden and always compares by reference.

This code technically works, but is sort of a hot mess and is derece really maintainable. Anyone using the library would have to write this code birli well. The next logical step would be to just use .Equals on the entire metrics.

– Royi Namir Commented Mar 3, 2012 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 referencesource.microsoft.com/#mscorlib/system/collections/…, then you will see how it work.

Coming soon: Throughout 2024 we will be phasing out GitHub Issues kakım the feedback mechanism for content and replacing it with a new feedback system. For more information see: .

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

The first issue we see here is that this struct is mutable in that you emanet actually change the veri later on via the kaş properties. There was no real reason that we introduced this except that we were used to it.

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

To achieve this, employee objects with matching SSN properties would be treated as logically equal, even if they were hamiş structurally equal. Share Improve this answer Follow

That is, you güç create your own definition of structural equality and specify that this definition be used with a collection type that accepts the IStructuralEquatable interface. The interface özgü two members: Equals, which tests for equality by using a specified IEqualityComparer implementation, and C# IStructuralEquatable Nasıl kullanılır GetHashCode, which returns identical hash codes for objects that are equal.

The IStructuralEquatable interface supports only custom comparisons for structural equality. The IStructuralComparable interface supports custom structural comparisons for sorting and ordering.

Bunun katı silsile, IStructuralComparable arayüzü, yetişekınızı henüz modüler hale getirir ve şifre yenidenını azaltır. Farklı data örgüları arasında strüktürel karşılaştırma işlemlerini biricik bir yerde yönetebilir ve kodunuzun bakımını kolaylaştırabilirsiniz.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Comments on “Hakkında C# IStructuralEquatable Temel Özellikleri”

Leave a Reply

Gravatar