为什么 CompareTo 这么短地实现?

发布于 2024-11-18 06:35:33 字数 915 浏览 3 评论 0原文

考虑以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难如初 2024-11-25 06:35:33

不同之处在于,对于 short,结果不可能溢出。例如,short.MinValue - (short) 1 仍然是负数 - 而 int.MinValue - 1int.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 - whereas int.MinValue - 1 is int.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 with int. You should definitely not require IComparable<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.

氛圍 2024-11-25 06:35:33

好吧,你应该只真正检查符号 无论如何,但出于以下原因:我猜对于 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 use short, but not anything like as much as I do int).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文