为什么比较运算符不会自动用IComparable重载? [英] Why are the comparison operators not automatically overloaded with IComparable?

查看:85
本文介绍了为什么比较运算符不会自动用IComparable重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当类为IComparable时,由于CompareTo功能,我们知道使<>==运算符重载的所有内容,对吗?那么为什么这些不自动超载呢?

When a class is IComparable, then we know everything to overload the <, > and == operators due to the CompareTo functionality, right? Then why aren't these overloaded automatically?

看看下面的例子:

public class MyComparable : IComparable<MyComparable>
{
    public int Value { get; }
    public MyComparable(int value) { Value = value; }

    public int CompareTo(MyComparable other) => Value.CompareTo(other.Value);
}

我想知道为什么默认情况下这样的事情不起作用:

I was wondering why something like this wouldn't work by default:

MyComparable obj1 = new MyComparable(1),
             obj2 = new MyComparable(2);

if (obj1 < obj2) { /*...*/ }

我们知道obj1 < obj2 == true是因为我们实现了CompareTo,但是因为<运算符没有被重载,所以这是行不通的. (我知道obj1.CompareTo(obj2) < 0会给出期望的结果,但是在大多数情况下不太明显.)

We know that obj1 < obj2 == true because of our implementation of CompareTo, but because the < operator is not overloaded, this will not work. (I know that obj1.CompareTo(obj2) < 0 will give the desired result, but that's less obvious in most cases.)

仅当我将以下代码添加到类中时,它才能按照我期望的方式工作:

Only when I add the code below to the class, it will work the way I expected:

public static bool operator <(MyComparable x, MyComparable y) => x.CompareTo(y) < 0;
public static bool operator >(MyComparable x, MyComparable y) => x.CompareTo(y) > 0;

// And for equality:
public static bool operator !=(MyComparable x, MyComparable y) => !(x == y);
public static bool operator ==(MyComparable x, MyComparable y)
{
    if (ReferenceEquals(x, y)) return true;
    if (((object) x == null) || ((object) y == null)) return false;
    return x.CompareTo(y) == 0;
}

这是非常通用的功能,那么为什么这些比较默认情况下不能在每个IComparable上起作用是什么原因?

This is very generic functionality, so what's the reason these comparisons don't work by default on every IComparable?

推荐答案

当一个类是IComparable时,由于CompareTo功能,我们就知道要重载<,>和==运算符的所有内容,对吧?

When a class is IComparable, then we know everything to overload the <, > and == operators due to the CompareTo functionality, right?

是的.

我分享你的无奈.您的投诉是我十大困扰C#的事情中的第九名.

I share your frustration. Your complaint is number nine on my list of ten things that irritate me about C#.

http://www.informit.com/articles/article.aspx?p=2425867

那为什么这些不自动超载呢?

Then why aren't these overloaded automatically?

默认情况下未实现功能,因此必须有理由将其删除.而是需要对功能进行设计,指定,实施,测试,记录和运输.如果这些事情之一没有发生,那就没有功能.

Features are not implemented by default and have to have reasons to remove them. Rather, features need to be designed, specified, implemented, tested, documented and shipped. If one of those things doesn't happen, no feature.

这些功能均未发生.该功能已被提出,但在语言设计团队所用的最佳用途列表中,它从来没有达到过顶峰.

None of those things happened for your desired feature. The feature has been proposed but never got anywhere close to the top of the list of best uses for the language design team's time.

如果您非常想使用此功能,可以随时在Roslyn Github论坛上介绍它,也许有一天会实现.

If you want this feature badly enough you can always pitch it at the Roslyn Github forum and maybe it will be implemented someday.

这篇关于为什么比较运算符不会自动用IComparable重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆