C# ISTRUCTURALEQUATABLE TEMEL ÖZELLIKLERI - GENEL BAKış

C# IStructuralEquatable Temel Özellikleri - Genel Bakış

C# IStructuralEquatable Temel Özellikleri - Genel Bakış

Blog Article

It's normally expected that if you implement IEquatable.Equals you will also override Object.Equals to be consistent. In this case how would you support both reference and structural equality?

= to provide value equality checks (vs the default reference equality check). The MSDN documentation suggests you only do it for immutable types. There are also issues involving interfaces and operator overloading.

g. MyType and Object) which will still use the identity comparison. I suspect it's not a great idea to do this unless it's going to be a very heavily used type in your code, where everyone will become very familiar with it and

LBushkinLBushkin 131k3333 gold badges217217 silver badges265265 bronze badges 11 8 Why gönül't you just specify an IEqualityComparer yourself that does this? What does the IStructuralEquatable interface add to this?

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

If two objects compare bey equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do hamiş have to return different values.

So, I am apparently wrong as unequal objects may have equal hash codes. But isn't GetHashCode returning a somewhat randomly distributed kaş of values a requirement?

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

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

When working with collections or structures where the order of elements matters, and you want to compare their structures, IStructuralEquatable can be useful.

C# IStructuralEquatable Defines methods to support the comparison C# IStructuralEquatable Nasıl kullanılır of objects for structural equality.

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 özgü a hawk/falcon/eagle type bird companion

Specifically, I do hamiş know the exact type of the object. The only assumption I make is that it inherit from IStructuralEquatable.

Report this page