If I dared to suggest, perhaps this is due to the support of using double values ββas keys in the dictionary.
If x.Equals(y) returned false for x = double.NaN and y = double.NaN , then you could have this code:
var dict = new Dictionary<double, string>(); double x = double.NaN; dict.Add(x, "These"); dict.Add(x, "have"); dict.Add(x, "duplicate"); dict.Add(x, "keys!");
I think most developers would find this behavior rather unintuitive. But it would be even more illogical:
// This would output false! Console.WriteLine(dict.ContainsKey(x));
Essentially, with an Equals implementation that never returns true for a particular value, you will have a type that can provide keys with the following weird behavior:
- Can be added to the dictionary an unlimited number of times.
- Could not be detected using
ContainsKey , and therefore ... - Can never be removed with
Remove
Remember that Equals very closely related to GetHashCode for this reason (the C # compiler even warns you if you override one without the other) - most of why they exist is to make it easier to use types as hash table keys.
As I said, this is just an assumption.
Dan Tao Feb 08 2018-11-11T00: 00Z
source share