Object equality behaves different in .NET

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

    As seen on C# FAQ on MSDN - the compiler cannot use the overloaded method and falls back to comparing the references.

    The bigger question is why it succeeds in the first object comparison. My best guess is that succeeds because a and b are both given the same reference. For c and d, you are forcing different references.

提交回复
热议问题