string a = "abc";
string b = "abc";
Console.WriteLine(String.ReferenceEquals(a, b));
a
and b
are CLEARLY different references, yet, I get true. Why is this? Somewhere I've read that when you assign abc
to b
, the compiler sees that there's already the exact abc
value in the heap and points to the same memory address as a
, but if this was the case, then by that logic the last line of this code below should print true
:
class SomeClass
{
public int _num;
public SomeClass(int num)
{
_num = num;
}
}
var a = new SomeClass(3);
var b = new SomeClass(3);
Console.WriteLine(Object.ReferenceEquals(a, b)); // prints false