5 Basit Teknikleri için C# IStructuralEquatable Kullanımı
Wiki Article
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.
Important Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
If you want to implement IEquatable in a class hierarchy you hayat use the following pattern. It prevents derived (including sibling) classes from being equal.
Does anyone know what happens if you do not implement iequtalable when using generic collections? 2
Your browser isn’t supported anymore. Update it to get the best YouTube experience and our latest features. Learn more
comparer IEqualityComparer An object that determines whether the current instance and other are equal.
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.
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...
Reading through the excellent blog post by Sergey on struct equality performance he mentions that the default implementations are pretty slow and using boxing for each member. Additionally, he mentions that a memory comparison may not give you the correct results in this super simple example:
If you read this entire post and are thinking wow that is a lot of code and steps to remember then do hamiş fear because Dustin told me and showed me that Visual Studio will generate all of this for you!!!!! Check this out:
C# IStructuralEquatable Defines methods to support the comparison of objects for structural equality.
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 güç just compare each of them":
Just look at the default ValueType.Equals(object) code that gets called otherwise. It's an absolute performance killer that introduces boxing, type C# IStructuralEquatable Temel Özellikleri evaluation and finally falls back on reflection if any of the fields are reference types.
Specifically, I do derece know the exact type of the object. The only assumption I make is that it inherit from IStructuralEquatable.