Object equality behaves different in .NET

前端 未结 9 2081
旧巷少年郎
旧巷少年郎 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:38

    Because they are two different object references. The built-in comparison for this is to compare whether they point to the same actual object or not.

    Due to String Interning, a and b are both references to the same string object.

    c==d is true because the string equality operator is being used.

提交回复
热议问题