My IEquatable still uses Object.GetHashcode for the dictionary <T> []

I have something like the following to be the key to the generic dictionary.

class IMyClass<T> : IEquatable<IMyClass> where T : struct
{
  //etc
}


class MyClass<T> : IMyClass<T> where T : struct
{
    public bool Equals(IRatingKey<T> other)
    {
       //etc
    }
}

From what I understand in EqualityComparer<T>.Default, he should see what I have implemented IEquatable<T>and, therefore, create EqualityComparer on the fly.

Dictionary<TKey, TValue>an equality implementation is required to determine if the keys are the same. If the comparison is set to null, the constructor uses the standard divisor equality EqualityComparer<T>.Default. If the type TKeyimplements a System.IEquatable<T>common interface, the equality equalizer uses this implementation by default.

However, from what I see in using a dictionary indexer Dictionary<T>[], it still relies on an override of GetHashcode, e.g.public override int GetHashCode()

, , . , IEquatable MyClass, IMyClass? IMyClass, .

IEqualityComparer, , , .

+4
1

Dictionary GetHashCode,

Dictionary Array L, ,

index = item.GetHashCode() % L

( , Abs )

,

---
 0  -> Item1, Item2
---
 1  -> Item3
---
 2 
---
...
---
L-1-> Item7

, Equality .

+2

Source: https://habr.com/ru/post/1526967/


All Articles