C# IStructuralEquatable Kullanımı - Genel Bakış

Wiki Article

That is, you güç create your own definition of structural equality and specify that this definition be used with a collection type that accepts the IStructuralEquatable interface.

In certain scenarios (such kakım using the value type kakım a key in a dictionary) it can murder performance in one foul swoop.

Reference types (read classes) don't benefit kakım 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:

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

Your concern is that Object.GetHashCode() does hamiş provide values that are stable and the concern is very valid as yaşama be seen in the first box headed by Caution in the documentation:

After some more testing I found that any two arrays with the same first element have the same hash. I still think this is strange behavior.

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...

The reason why you need the IStructuralEquatable is for defining a new way of comparision that would be right for all the objects .

Bir önceki medarımaişetlemin aynkaloriı AsOrdered extensionı ile kuruluşldığında muamelat gene muvazi olarak strüktürlır, fakat özlar sıralı olur.

Ancak, fruits1 ve fruits3 dizileri aynı elemanlara farklı sıralarda malik olduğundan, CompareTo metodu farklı bir kıymet döndürür ve bu dizilerin yapısal olarak hemayar olmadığını belirtir.

Bu mimariya kadar oluşturduğumuz bütün nesnelerin Heap kısmında bulunduğunu söylemiştik. Halbuki Stack kısmında struct strüktürsında nesneleri tutabilmekteyiz.

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

Being able to specify IStructuralEquatable/IStructuralComparable in such cases is actually C# IStructuralEquatable Nasıl kullanılır useful. It would also be inconvenient to pass a TupleComparer or ArrayComparer everywhere you want to apply this type of comparison. The two approaches are hamiş mutually exclusive.

Report this wiki page