Fei said:
Hi,
take string class as an example, who can explain the difference?
In the case of string, == will return true if both strings contain the
same value. ReferenceEquals returns true if both strings are the same
instance. Here is a code example:
string a = new string('a',3);
string b = new string('a',3);
string c = a;
Console.WriteLine(a==b); // True
Console.WriteLine(object.ReferenceEquals(a,b)); // False
Console.WriteLine(a==c); // True
Console.WriteLine(object.ReferenceEquals(a,c)); // True
In the case above, both a and b will contain the value "aaa". However
they are not the same instance. Both a and c will also contain the
value "aaa", but they are also the same instance.