What is the difference between value and reference equality for strings?
Short answer
string overrides == and Equals to compare characters, so two strings with the same content are equal even if they are different objects.
Advertisement
AModel answer
string overrides == and Equals to compare characters, so two strings with the same content are equal even if they are different objects.
var a = new string(new[]{'h','i'});
Console.WriteLine(a == "hi"); // True
Console.WriteLine(ReferenceEquals(a,"hi")); // FalseReal-world example
Comparing user input to an expected token by value, not reference.