Why in this situation ReferenceEquals
method of object behaves differently?
string a= "fg";
string b= "fg";
Console.WriteLine(object.ReferenceEquals(a, b));
So in this situation it's get a result true
. In case, it compares values of my strings and not references. But when I write something like:
StringBuilder c = new StringBuilder("fg");
string d = c.ToString();
Console.WriteLine(object.ReferenceEquals(a, d));
In this case it works fine and result is false
, because it compares references of my objects.
According to this post it has to do with something called interning. a and b are in your case two variables pointing to the same instance, that's why ReferenceEquals is returning true.
The first example has a compile time constant
"fg"
that is referenced by two variables. Since this is a compile time constant, the two variables reference the one object. The references are equal.Read into the topic of string interning for more on this behavior. As a starter, consider:
http://msdn.microsoft.com/en-us/library/system.string.intern.aspx
In the second example, only one is a compile time constant, the other is a result of some operations.
a
andd
do not reference the same object, so you get the false result fromReferenceEquals
.Since you reference the same literal ("fg"), both your strings will actually point at the same thing. Please take a look at this article: http://csharpindepth.com/Articles/General/Strings.aspx (paragraph "Interning").
Regards, Piotr
It is behaving correctly in both cases.
The reason
a
andb
are the same string object is because the compiler has noticed that you specified the same string twice, and has reused the same string object to initialize botha
andb
.This will generally happen with every string constant in your application.