運算符重載一直是一個很詭異事情,因為在寫代碼的時候,不知道某個運算符有沒有被重載過。在 C++ 里面,運算符重載可以寫在類的外面,當 intellisense 不工作的時候,找到一個運算符的重載函數是一件相當頭疼的事情。這個問題在 C# 中改善了不少,因為運算符重載一定要寫在類內,而且 intellisense 很強大。不過另一個問題又產生了……
先來看 C++ 中的“==”重載:
struct A{ int x; int y;}; inlinebool operator == (const A& a, const A& b){ return a.x == b.x && a.y == b.y;}
上面這段代碼中,由于聲明的關系,a 和 b 永遠不可能為 NULL,所以直接調用 a.x 和 b.x 是沒有問題的。
而在 C# 中:
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 就有問題了,比如:
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 的實現也很詭異:
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 不建議重載==,但是不建議并不代表不能用吧,這個設計太挫了…
新聞熱點
疑難解答