Check this out :
var a = Double.NaN;
Console.WriteLine(a == a);
Console.ReadKey();
Prints \"False\"
var a
If I were to venture a guess, it might be that this is to support the use of double
values as keys in a dictionary.
If x.Equals(y)
returned false
for x = double.NaN
and y = double.NaN
, then you could have code like this:
var dict = new Dictionary();
double x = double.NaN;
dict.Add(x, "These");
dict.Add(x, "have");
dict.Add(x, "duplicate");
dict.Add(x, "keys!");
I think the majority of developers would find this behavior rather unintuitive. But even more counterintuitive would be this:
// This would output false!
Console.WriteLine(dict.ContainsKey(x));
Basically, with an implementation of Equals
that never returns true
for a certain value, what you would have is a type capable of providing keys with the following bizarre behavior:
ContainsKey
, and therefore...Remove
Remember that Equals
is very closely related to GetHashCode
for this very reason (the C# compiler even warns you if you've overridden one without the other)—a big part of why they're there in the first place is to facilitate the use of types as hash table keys.
Like I said, it's just a guess.