Consider the following code:
namespace ConsoleApplication1 {
class Program
{
static void Main(string[] args)
{
Console.WriteLine(100.CompareTo(200));
Console.WriteLine(((decimal)100).CompareTo((decimal)200));
Console.WriteLine(((short)100).CompareTo((short)200));
Console.WriteLine(((float)100).CompareTo((float)200));
Console.ReadKey();
}
}
}
My question is, are there any specific reasons for the CompareTo method on Int16 to return values other than -1, 0, and 1?
ILSpy shows that it is implemented in this way
public int CompareTo(short value)
{
return (int)(this - value);
}
whereas the method is implemented on Int32 in this way
public int CompareTo(int value)
{
if (this < value)
{
return -1;
}
if (this > value)
{
return 1;
}
return 0;
}
source
share