Why is == overridden in System.Double but not in System.Int32 and what are the ramifications of that?
c#, operator-overloading
Solution
One possible reason is because of the Double.NaN.
For the == operator: MSDN says: If two Double.NaN values are tested for equality by using the equality operator(==), the result is false; two Double.NaN values are not considered equal. If they are tested for equality by calling the Equals method, the result is true. When you want to determine whether the value of a Double is not a number (NaN), an alternative is to call the IsNaN method.
So the == operator and the Equals methods of Double have different behavior regards to Double.NaN, I think this why == is override for double. As for int, there's no such special case.
The code to demo the differences:
using System;
public class Example
{
public static void Main()
{
Console.WriteLine("NaN == NaN: {0}", Double.NaN == Double.NaN);
Console.WriteLine("NaN != NaN: {0}", Double.NaN != Double.NaN);
Console.WriteLine("NaN.Equals(NaN): {0}", Double.NaN.Equals(Double.NaN));
Console.WriteLine("! NaN.Equals(NaN): {0}", ! Double.NaN.Equals(Double.NaN));
Console.WriteLine("IsNaN: {0}", Double.IsNaN(Double.NaN));
Console.WriteLine("\nNaN > NaN: {0}", Double.NaN > Double.NaN);
Console.WriteLine("NaN >= NaN: {0}", Double.NaN >= Double.NaN);
Console.WriteLine("NaN < NaN: {0}", Double.NaN < Double.NaN);
Console.WriteLine("NaN < 100.0: {0}", Double.NaN < 100.0);
Console.WriteLine("NaN <= 100.0: {0}", Double.NaN <= 100.0);
Console.WriteLine("NaN >= 100.0: {0}", Double.NaN > 100.0);
Console.WriteLine("NaN.CompareTo(NaN): {0}", Double.NaN.CompareTo(Double.NaN));
Console.WriteLine("NaN.CompareTo(100.0): {0}", Double.NaN.CompareTo(100.0));
Console.WriteLine("(100.0).CompareTo(Double.NaN): {0}", (100.0).CompareTo(Double.NaN));
}
}
// The example displays the following output:
// NaN == NaN: False
// NaN != NaN: True
// NaN.Equals(NaN): True
// ! NaN.Equals(NaN): False
// IsNaN: True
//
// NaN > NaN: False
// NaN >= NaN: False
// NaN < NaN: False
// NaN < 100.0: False
// NaN <= 100.0: False
// NaN >= 100.0: False
// NaN.CompareTo(NaN): 0
// NaN.CompareTo(100.0): -1
// (100.0).CompareTo(Double.NaN): 1
The code is also from MSDN
Problem
In C# Why does `Double` override `==` but `Int32` does not, and what is the effect? I look at the msdn library. I see this link about double which doesn't say much here (though I understand double is a shorthand for the `Double` object).. It doesn't show methods for example.. but this link on `System.Double` does mention what I'm looking for here It shows the Equality operator taking doubles, so overloaded. Image For `Double` in MSDN, then after Methods (before listing Fields), it shows Operators, and it shows equality operator is overridden and I can click Equality under "Operators" and it says ``` public static bool operator ==( double left, double right ) ``` Whereas when I look at `System.Int32` Image For `Int32` in MSDN is below See in that image, (the page for `System.Int32`) it looks like `==` is not overridden. Why, and what are the ramifications of this?