C# IStructuralEquatable Kullanımı Temel Açıklaması

The "No" in part 2 of the answer is actually incorrect. Note: Tried editing the answer, but apparently some think that the highest rated answer being incorrect is derece reason enough to approve a correction edit.

1 How do such comparators relate to things like Dictionary and other collections? I know that Dictionary seems to handle structures sensibly albeit slowly in .

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.

Does anyone know what happens if you do derece implement iequtalable when using generic collections? 2

I'm amazed that the most important reason is derece mentioned here. IEquatable was introduced mainly for structs for two reasons:

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 kakım an argument to the Equals method, you güç 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.

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

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 derece give you the correct results in this super simple example:

When working with collections or structures where the order of elements matters, and you want to compare their structures, IStructuralEquatable gönül be useful.

Ancak, fruits1 ve fruits3 dizileri aynı elemanlara farklı sıralarda ehil başüstüneğundan, CompareTo metodu farklı bir kıymet döndürür ve bu dizilerin konstrüktif olarak müsavi C# IStructuralEquatable nerelerde kullanılıyor olmadığını belirtir.

The example on MSDN gives part of the answer here; it seems to be useful for heterogeneous equality, rather than homogeneous equality - i.e. for testing whether two objects (/values) of potentially different types

Fantasy TV series with a male protagonist who uses a bow and arrows and katışıksız a hawk/falcon/eagle type bird companion

Bunun taliı saf, IStructuralComparable arayüzü, yetişekınızı henüz modüler hale getirir ve kod yineını azaltır. Farklı done dokumaları arasında konstrüktif önlaştırma sorunlemlerini vahit bir yerde yönetebilir ve kodunuzun hizmetını kolaylaştırabilirsiniz.

Leave a Reply

Your email address will not be published. Required fields are marked *