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 birli using the value type bey a key in a dictionary) it gönül murder performance in one foul swoop.
Reference types (read classes) don't benefit as 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.
Do hamiş fear because if you simply implement IEquatable the dictionary will use the strongly typed version! The nice thing is that we kind of actually already did this! So now we just have to do this:
Although I think the gains from hamiş boxing will be less than the cost for having CanEqual. In that case you should seal your types and you no longer need CanEqual. Sealing also özgü some performance benefits.
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 emanet define a custom equality comparison for the array or collection.
What does IEquatable buy you, exactly? The only reason I güç see it being useful is when creating a generic type and forcing users to implement and write a good equals method.
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: .
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 derece fear because Dustin told me and showed me that C# IStructuralEquatable Kullanımı Visual Studio will generate all of this for you!!!!! Check this out:
This member is an explicit interface member implementation. It birey be used only when the Array instance is cast to an IStructuralEquatable interface.
Will feeblemind affect the original creature's body when it was cast on it while it was polymorphed and reverted to its original form afterwards?
Fantasy TV series with a male protagonist who uses a bow and arrows and has a hawk/falcon/eagle type bird companion
However, this is not so great if you are using the struct in a dictionary bey my good friend Dustin mentioned to me because a Dictionary will always use the object version of Equals, which falls back to boxing :(