Object equality behaves different in .NET

前端 未结 9 2087
旧巷少年郎
旧巷少年郎 2020-12-07 01:58

I have these statements and their\' results are near them.

string a = \"abc\";
string b = \"abc\";

Console.Writeline(a == b); //true

object x = a;
object y         


        
9条回答
  •  没有蜡笔的小新
    2020-12-07 02:57

    The == operator compares references (the memory addresses) while .Equals compares the actual object values. In the case of string, you get lucky and two identical strings can refer to the same address frequently. One of the joys of managed languages.

提交回复
热议问题