2014-02-14 107 views

回答

5

您正在創建兩個字節的數組。兩個陣列具有不同的存儲器地址並比較不同的地址返回錯誤

bool result = (new byte[0] == new byte[0]); 
Console.WriteLine(result); 

...... 
IL_0001: ldc.i4.0     // zero int for size in the evaluation stack 
IL_0002: newarr  System.Byte  // create an array of zero bytes 
IL_0007: ldc.i4.0     // zero int for size in the evaluation stack 
IL_0008: newarr  System.Byte  // create another array of zero bytes 
IL_000D: ceq       // compare the address of the two arrays 
IL_000F: stloc.0  // result 
IL_0010: ldloc.0  // result 
IL_0011: call  System.Console.WriteLine 
0

數組是參考類型。每個實例都有自己的參考,這意味着它們不會相同。

2

您正在創建兩個新數組並進行參考比較。當我說參考時,我的意思是每個人生活在記憶中的位置。由於它們不是一回事,它總是會失敗。

如果你願意,它會返回true;

byte[] a = new byte[0]; 
    byte[] b = a; 
    return a == b; 
相關問題