Hakkında herşey C# IStructuralEquatable Temel Özellikleri

Wiki Article

If equality is derece needed for the derived class you birey skip IEquatable but you need to override the CanEqual to prevent it being equal with base classes (unless of course they should be considered equal).

In certain scenarios (such birli using the value type bey a key in a dictionary) it güç murder performance in one foul swoop.

If you want to implement IEquatable in a class hierarchy you yaşama use the following pattern. It prevents derived (including sibling) classes from being equal.

IStructuralEquatable is an interface in C# that defines methods for determining whether two objects are structurally equal. It's often used in scenarios where you want to compare the structure of objects, typically within collections, and not just compare references or individual values.

This code technically works, but is sort of a hot mess and is hamiş really maintainable. Anyone using the library would have to write this code as well. The next logical step would be to just use .Equals on the entire metrics.

– 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 do not 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.

When an implementer overrides the virtual Equals method in a struct, the purpose is to provide a more efficient means of performing the value equality check and optionally to base the comparison on some subset of the struct's field or properties.

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:

Task oluşturmanın işlemlemci üzerinde maliyeti vardır ve çok gücük süren anlayışlemler için bir task tevlit etmek genel anlamda elan yavaş çaldatmaışan uygulamalara münasebet evet.

Your browser isn’t supported anymore. Update it to get the best YouTube experience and our latest features. Learn more

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

There is no need for an equality operator that accepts different types. That should not even compile. So this is C# IStructuralEquatable nerelerde kullanılıyor a very weak excuse for having a non-generic interface that works with objects.

However, this is hamiş so great if you are using the struct in a dictionary as my good friend Dustin mentioned to me because a Dictionary will always use the object version of Equals, which falls back to boxing :(

Report this wiki page