Için basit anahtar C# IStructuralEquatable Kullanımı örtüsünü
Için basit anahtar C# IStructuralEquatable Kullanımı örtüsünü
Blog Article
If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do derece compare as equal, the GetHashCode methods for the two object do derece have to return different values.
= to provide value equality checks (vs the default reference equality check). The MSDN documentation suggests you only do it for immutable types. There are also issues involving interfaces and operator overloading.
Reference types (read classes) don't benefit bey 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.
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 birli is documented behavior, some reasoning and reflection would also lead you to the same conclusion.
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.
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 birli an argument to the Equals method, you can 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.
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 reason why you need the IStructuralEquatable is for defining a new way of comparision that would be right for all the objects .
Collaborate with us on GitHub The source for this content kişi be found on GitHub, where you birey also create and review issues and pull requests. For more information, see our contributor guide.
This member is an explicit interface member implementation. It birey be used only when the Array instance is cast to an IStructuralEquatable interface.
Now that our struct is immutable the actual issue comes up when you need to compare these values. When I started to write the code to fix the bug I just decided that "hey I have the old values, I hayat just compare each of them":
Are the bonuses for infernal war machine weapon stations static, or are they affected by their user?
Burada uyanıklık ederseniz eğer ms.x ve C# IStructuralEquatable Kullanımı ms.y değkonumeyeceğinden dolayı bir Deep Copy nazire konusudur. şayet referans tipli gestaltlar olsalardı zirdaki gibi bir sonuç elde edilecekti ve Shallow Copy söz konusu olacaktı.