0

there is some diff between == & Referencequals

see following code:

string s1 = "1";
string s2 = string.Copy("1");

Console.WriteLine(object.ReferenceEquals(s1, s2));
Console.WriteLine(s1 == s2);
Console.Read();

The output is:

False
True

Can anybody explain this behaviour??

1 Answers1

9

ReferenceEquals compares the references of the objects.

== can be overloaded to do whatever the type wants. The default behavior for reference types, if it's not overloaded, is a comparison of the reference. String overloaded the operator to do a value based comparison instead of a reference comparison.

Servy
  • 202,030
  • 26
  • 332
  • 449