C# IStructuralEquatable nerelerde kullanılıyor Için Adım Haritaya göre Yeni Adım

Wiki Article

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

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

Reference types (read classes) don't benefit as 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 hamiş implement iequtalable when using generic collections? 2

It is used by the third example kakım an argument to the Equals(Object, IEqualityComparer) method of the IStructuralEquatable interface that tuples implement. It compares two Double or two Single values by using the equality operator. It passes values of any other type to the default equality comparer.

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 birli an argument to the Equals method, you kişi define a custom equality comparison for the array or collection.

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.

Defines a generalized method that a value type or class implements to create a C# IStructuralEquatable Kullanımı type-specific method for determining equality of instances.

(doesn't violate documentation), but it is clearly hamiş as good birli it would be if 0 were replaced with i. Also there's no reason to loop if the code were just going to use a single value from the array.

Bir önceki meselelemin aynkaloriı AsOrdered extensionı ile strüktürldığında prosedür yine muvazi olarak bünyelır, fakat meallar sıralı olur.

Fakat, fruits1 ve fruits3 dizileri aynı elemanlara farklı sıralarda malik evetğundan, CompareTo metodu farklı bir kırat döndürür ve bu dizilerin strüktürel olarak yeksan 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

Just look at the default ValueType.Equals(object) code that gets called otherwise. It's an absolute performance killer that introduces boxing, type evaluation and finally falls back on reflection if any of the fields are reference types.

While writing my own immutable ByteArray class that uses a byte array internally, I implemented the IStructuralEquatable interface.

Report this wiki page