Default IEqualityComparer faster than a hand-written one for dictionary with Enum type as key?

Viewed 35

For some odd reason, it seems a hand-written equality comparer for a specific enum is slower than the default equality comparer a Dictionary<TKey,TValue> uses.

How is that possible? Tested on .NET 6.0.

The hand-written equality comparer:

public class EnumEqualityComparer : IEqualityComparer<DayOfWeek>
{
    public bool Equals(DayOfWeek x, DayOfWeek y) => x == y;

    public int GetHashCode([DisallowNull] DayOfWeek obj) => obj.GetHashCode();
}
0 Answers
Related