为什么 CompareTo 这么短地实现?
考虑以下代码:
namespace ConsoleApplication1 {
class Program
{
static void Main(string[] args)
{
Console.WriteLine(100.CompareTo(200)); // prints -1
Console.WriteLine(((decimal)100).CompareTo((decimal)200)); // prints -1
Console.WriteLine(((short)100).CompareTo((short)200)); // prints -100
Console.WriteLine(((float)100).CompareTo((float)200)); // prints -1
Console.ReadKey();
}
}
}
我的问题是,Int16 上的 CompareTo 方法返回 -1、0 和 1 以外的值是否有任何具体原因?
ILSpy 显示它是这样实现的,
public int CompareTo(short value)
{
return (int)(this - value);
}
而该方法是实现的在 Int32 上这样
public int CompareTo(int value)
{
if (this < value)
{
return -1;
}
if (this > value)
{
return 1;
}
return 0;
}
Consider the following code:
namespace ConsoleApplication1 {
class Program
{
static void Main(string[] args)
{
Console.WriteLine(100.CompareTo(200)); // prints -1
Console.WriteLine(((decimal)100).CompareTo((decimal)200)); // prints -1
Console.WriteLine(((short)100).CompareTo((short)200)); // prints -100
Console.WriteLine(((float)100).CompareTo((float)200)); // prints -1
Console.ReadKey();
}
}
}
My question is, are there any specific reasons the CompareTo-method on Int16 returns values other than -1, 0 and 1?
ILSpy shows it is implemented this way
public int CompareTo(short value)
{
return (int)(this - value);
}
whereas the method is implented on Int32 this way
public int CompareTo(int value)
{
if (this < value)
{
return -1;
}
if (this > value)
{
return 1;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不同之处在于,对于
short
,结果不可能溢出。例如,short.MinValue - (short) 1
仍然是负数 - 而int.MinValue - 1
是int.MaxValue
。换句话说,具体原因是您可以使用
short
(没有双关语)的快捷方式,而相同的快捷方式不适用于int
。您绝对不应该要求IComparable.CompareTo
实现返回 -1、0 或 1。文档非常清楚,结果仅在负数、零或正数方面才有意义。The difference is that for
short
, there's no chance of the result overflowing. For instance,short.MinValue - (short) 1
is still negative - whereasint.MinValue - 1
isint.MaxValue
.In other words, the specific reason is that you can get away with a shortcut with
short
(no pun intended) whereas the same shortcut doesn't work withint
. You should definitely not requireIComparable<T>.CompareTo
implementations to return -1, 0 or 1. The documentation is pretty clear that the result is only meaningful in terms of being negative, zero, or positive.好吧,你应该只真正检查符号 无论如何,但出于以下原因:我猜对于
int
等,会有溢出/换行的风险(当处理 2 个大数值时)数字)会反转符号,这意味着它必须检查运算符。我宁愿它是一致的,但这似乎不是问题。更有可能的是一种非典型的优化,但在记录的 API 内。特别是,在这里优化
short
感觉不会得到大量的使用(我使用short< /code>,但不像我那样int
)。Well, you should only really check the sign anyway, but for reasons: I guess for
int
etc there would be a risk of overflow/wrap (when handling 2 large-magnitude numbers) that would reverse the sign, meaning it must check the operators.I'd rather it was consistent, but it doesn't seem to be a problem. More likely an optimisation that is atypical but within the documented API. In particular, optimising
short
here doesn't feel like it is going to get a massive amount of use (I useshort
, but not anything like as much as I doint
).