close
VS 裡的 ==, 與 Object.Equals 是不同的.
來個程式碼吧, 這樣比較好解釋.
object a1 = 1;
object a2 = 1;
Console.WriteLine(string.Format("object a1==a2 {0}", a1 == a2));//false
Console.WriteLine(string.Format("object a1.Equals(a2) {0}", a1.Equals(a2)));//true
Console.WriteLine();
object s1 = "s1";
object s2 = "s1";
Console.WriteLine(string.Format("object s1==s2 {0}", s1 == s2));//true
Console.WriteLine(string.Format("object s1.Equals(s2) {0}", s1.Equals(s2)));//true
Console.WriteLine();
object r1 = new List<string>();
object r2 = new List<string>();
Console.WriteLine(string.Format("List<string> r1==r2 {0}", r1 == r2));//false
Console.WriteLine(string.Format("List<string> r1.Equals(r2) {0}", r1.Equals(r2)));//false
Console.ReadKey();
來個程式碼吧, 這樣比較好解釋.
object a1 = 1;
object a2 = 1;
Console.WriteLine(string.Format("object a1==a2 {0}", a1 == a2));//false
Console.WriteLine(string.Format("object a1.Equals(a2) {0}", a1.Equals(a2)));//true
Console.WriteLine();
object s1 = "s1";
object s2 = "s1";
Console.WriteLine(string.Format("object s1==s2 {0}", s1 == s2));//true
Console.WriteLine(string.Format("object s1.Equals(s2) {0}", s1.Equals(s2)));//true
Console.WriteLine();
object r1 = new List<string>();
object r2 = new List<string>();
Console.WriteLine(string.Format("List<string> r1==r2 {0}", r1 == r2));//false
Console.WriteLine(string.Format("List<string> r1.Equals(r2) {0}", r1.Equals(r2)));//false
Console.ReadKey();
以上程式碼, 想來想去, a1==a2 是 false, 怎樣也想不通.
MSDN 對於 == 的說明很詳細, 也有範例.
簡單的說就是這樣 : 對於 string 以外的參考型別,若兩個運算元參考到同一物件,== 會傳回 true。對於 string 型別,== 會比較字串的值。
另再參考 MSDN 多載 Equals() 和運算子 == 的方針 這樣就解了.
在 C# 中,相等代表著兩種不同的意義,可能是參考相等或實值相等。實值相等是我們對相等的一般認知,意即兩個物件包含相同的值。例如,兩個值為 2 的整數即視為實值相等。參考相等表示不存在兩個可比較的物件,而是存在有參考同一物件的兩個物件參考。
理解實際的定義後, 再看原來的程式碼, 及藍色字體的說明, 這樣就清楚多了.
理解實際的定義後, 再看原來的程式碼, 及藍色字體的說明, 這樣就清楚多了.
object a1 = 1;
object a2 = 1;
Console.WriteLine(string.Format("object a1==a2 {0}", a1 == a2));//false, 非 string 比 reference
Console.WriteLine(string.Format("object a1.Equals(a2) {0}", a1.Equals(a2)));//true, Value type 比值
Console.WriteLine();
object s1 = "s1";
object s2 = "s1";
Console.WriteLine(string.Format("object s1==s2 {0}", s1 == s2));//true, string 比值
Console.WriteLine(string.Format("object s1.Equals(s2) {0}", s1.Equals(s2)));//true, Value type 比值
Console.WriteLine();
object r1 = new List<string>();
object r2 = new List<string>();
Console.WriteLine(string.Format("List<string> r1==r2 {0}", r1 == r2));//false, 非 string 比 reference
Console.WriteLine(string.Format("List<string> r1.Equals(r2) {0}", r1.Equals(r2)));//false, reference type 比reference
Console.ReadKey();
object a2 = 1;
Console.WriteLine(string.Format("object a1==a2 {0}", a1 == a2));//false, 非 string 比 reference
Console.WriteLine(string.Format("object a1.Equals(a2) {0}", a1.Equals(a2)));//true, Value type 比值
Console.WriteLine();
object s1 = "s1";
object s2 = "s1";
Console.WriteLine(string.Format("object s1==s2 {0}", s1 == s2));//true, string 比值
Console.WriteLine(string.Format("object s1.Equals(s2) {0}", s1.Equals(s2)));//true, Value type 比值
Console.WriteLine();
object r1 = new List<string>();
object r2 = new List<string>();
Console.WriteLine(string.Format("List<string> r1==r2 {0}", r1 == r2));//false, 非 string 比 reference
Console.WriteLine(string.Format("List<string> r1.Equals(r2) {0}", r1.Equals(r2)));//false, reference type 比reference
Console.ReadKey();
全站熱搜