C# › Fundamentals

What is the difference between value and reference equality for strings?

Beginner

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")); // False
Real-world example Comparing user input to an expected token by value, not reference.