5 Basit Teknikleri için C# IStructuralEquatable Kullanımı
Wiki Article
comparer IEqualityComparer An object that determines whether the current instance and other are equal.
In certain scenarios (such as using the value type kakım a key in a dictionary) it sevimli murder performance in one foul swoop.
Reference types (read classes) don't benefit kakım much. The IEquatable implementation does let you avoid a cast from System.Object but that's a very trivial gain. I still like IEquatable to be implemented for my classes since it logically makes the intent explicit.
Default property. The second time, it passes the default equality comparer that is returned by the StructuralComparisons.StructuralEqualityComparer property. The third time, it passes the custom NanComparer object. Kakım the output from the example shows, the first three method calls return true, whereas the fourth call returns false.
It is used by the third example as an argument to the Equals(Object, IEqualityComparer) method of the IStructuralEquatable interface that tuples implement. It compares two Double or two Single values by using the equality operator. It passes values of any other type to the default equality comparer.
– 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.
If those objects C# IStructuralEquatable Temel Özellikleri do hamiş contain equality/hashcode methods that satisfy that contract, you will have to wrap them and provide correct implementations for those methods yourself in the wrapper.
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...
Ray BooysenRay Booysen 29.6k1414 gold badges8686 silver badges111111 bronze badges 6 so when you are dealing with objects, is == assumed to only mean the exact same memory address (same instance)
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 saf 8 upvotes, it is wrong. After a lot of 'reflector'ing, here is my take on things.
Each of your objects should use a hashcode based on the contents of the object. If you have a value type containing 3 ints, use those when computing the hash code. Like this, all objects with identical content will have the same hash code, independent of app domain and other circumstances.
Olur, makalede eksiklikler mümkün. Bunu eleştiri olarak ikrar ediyorum. Fonksiyonel programlamada struct kullanılmasının nedenini bile bilmiyor, performans ve başka konular üzerine da herhangi bir rey barındırmıversiyon. Siz biliyorsanız lütfen aydınlatır mısınız?
The IStructuralEquatable interface supports only custom comparisons for structural equality. The IStructuralComparable interface supports custom structural comparisons for sorting and ordering.
While writing my own immutable ByteArray class that uses a byte array internally, I implemented the IStructuralEquatable interface.