Için basit anahtar C# IStructuralEquatable nerelerde kullanılıyor örtüsünü
Için basit anahtar C# IStructuralEquatable nerelerde kullanılıyor ö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 kakım equal, the GetHashCode methods for the two object do not 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.
I'm amazed that the most important reason is hamiş mentioned here. IEquatable was introduced mainly for structs for two reasons:
Your concern is that Object.GetHashCode() does derece provide values that are stable and the concern is very valid kakım güç be seen in the first box headed by Caution C# IStructuralEquatable Nasıl kullanılır in the documentation:
Coming soon: Throughout 2024 we will be phasing out GitHub Issues birli the feedback mechanism for content and replacing it with a new feedback system. For more information see: .
Coming soon: Throughout 2024 we will be phasing out GitHub Issues bey the feedback mechanism for content and replacing it with a new feedback system. For more information see: .
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)
If you read this entire post and are thinking wow that is a lot of code and steps to remember then do not fear because Dustin told me and showed me that Visual Studio will generate all of this for you!!!!! Check this out:
To achieve this, employee objects with matching SSN properties would be treated bey logically equal, even if they were hamiş structurally equal. Share Improve this answer Follow
The contract of Equals differs from that of IStructuralEquatable, in that it indicates whether 2 objects are logically equal.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
While writing my own immutable ByteArray class that uses a byte array internally, I implemented the IStructuralEquatable interface.