15

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.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123

5 Answers5

21

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:

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

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 and d do not reference the same object, so you get the false result from ReferenceEquals.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
3

It is behaving correctly in both cases.

The reason a and b 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 both a and b.

This will generally happen with every string constant in your application.

Ben
  • 34,935
  • 6
  • 74
  • 113
2

It is beacuse of the CLR with version above 4.5 assemblies are marked with attribute System.Runtime.CompilerServices.CompilationRelaxations Attribute which defines value of flag System.Runtime.CompilerServices. CompilationRelaxations.NoStringInterning.This was implemented for the purpose to improve performance.

If run your code in CLR version below 4.5, variables a and b will reference to different string-objects in the heap with value of "fg" and object.ReferenceEquals(a, b) will give result "False".

Since CLR 4.5 the comparison of object.ReferenceEquals(a, b) will give result "True", because it interns "fg" string while uploading assembly to application domain. It means that a and b references to same string in the heap.

Philip Voloshin
  • 101
  • 1
  • 3
0

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

Piotr Justyna
  • 4,888
  • 3
  • 25
  • 40
0

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.

granaker
  • 1,318
  • 8
  • 13