运算符重载一直是一个很诡异事情,因为在写代码的时候,不知道某个运算符有没有被重载过。在C++里面,运算符重载可以写在类的外面,当intellisense不工作的时候,找到一个运算符的重载函数是一件相当头疼的事情。这个问题在C#中改善了不少,因为运算符重载一定要写在类内,而且intellisense很强大。不过另一个问题又产生了……

先来看C++中的“==”重载:

1
2
3
4
5
6
7
8
9
struct A{
    int x;
    int y;
};

inline
bool operator == (const A& a, const A& b){
    return a.x == b.x && a.y == b.y;
}

上面这段代码中,由于声明的关系,a和b永远不可能为NULL,所以直接调用a.x和b.x是没有问题的。

而在C#中:

1
2
3
4
5
6
7
8
9
10
11
12
struct A
{
    public int x, y;
    public static bool operator ==(A a, A b)
    {
        return a.x == b.x && a.y == b.y;
    }
    public static bool operator !=(A a, A b)
    {
        return !(a == b);
    }
}

这段代码是没问题的,因为A是struct,而struct不可能为null。但换成class就有问题了,比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    class A
    {
        public int x, y;
        public static bool operator ==(A a, A b)
        {
            if (a == null && b == null)
            {
                return true;
            }
            if (a == null || b == null)
            {
                return false;
            }
            return a.x == b.x && a.y == b.y;
        }
        public static bool operator !=(A a, A b)
        {
            return !(a == b);
        }
    }

由于reference type可以为null,所以要先检查a和b是不是null,但是“a == null”这一句又会去调用“operator ==”,于是就无限递归下去了……想了很久都没想出来变通的方法,而且System.String的实现也很诡异:

1
2
3
4
5
6
7
8
public static bool operator ==(string a, string b)
{
    return Equals(a, b);
}
public static bool Equals(string a, string b)
{
    return ((a == b) || (((a != null) && (b != null)) && EqualsHelper(a, b)));
}

看上去也会无限递归的(Reflector出来的,不一定准),很神奇……

虽然对于Referece type不建议重载==,但是不建议并不代表不能用吧,这个设计太挫了……

您还可能感兴趣的日志:

  1. 在C#中使用@声明变量 – 逐字标识符
  2. Hyper-V的自动化 (1) 获取虚拟机信息
  3. 从32位应用程序访问64位注册表
  4. 经典面试题 之 单链表找环
  5. 经典面试题 之 查字典
  6. Hyper-V的自动化 (0) 远程连接
  7. 经典面试题 之 按单词逆序
  8. PowerShell Cmdlet的测试 (二) 获取结果
  9. 经典面试题 之 洗牌问题
  10. 经典面试题 之 数组的循环右移

Sorry, the comment form is closed at this time.

 
© 2004 - 2011 Leona+Suffusion theme by Sayontan Sinha