2015-09-23 107 views

回答

91

不,它不等效 - 因爲Version超載==運算符。

其蒙上左操作數Object的片段是等價於:

if (Object.ReferenceEquals(version, null)) 

...而不是調用Versionoperator==實施。無論如何,這可能會使無效檢查成爲其第一個操作,但這只是繞過額外的級別。

在其他情況下,這可能會產生非常顯着的差異。例如:

string original = "foo"; 
string other = new string(original.ToCharArray()); 
Console.WriteLine(original == other); // True 
Console.WriteLine((object) original == other); // False 
+2

@afaolek:無需經過源代碼 - 只需查看文檔,您就會看到相等運算符。 –

+0

在你的答案中使用樣本,Console.WriteLine((對象)原始==(對象)其他)的結果是什麼;'? – afaolek

+1

@afaolek:再次假 - 這不會改變任何東西,因爲它基本上仍然會使用參考比較。 –