Share via


c#: Difference between ==, object.ReferenceEquals and Equals

This is the basic difference between ==, ReferenceEquals() and .Equals()

Sample Script

string s1 = "test";

string s2 = "test";

string s3 = "test1".Substring(0, 4);

object s4 = s3;

Console.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s2), s1 == s2, s1.Equals(s2));

Console.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s3), s1 == s3, s1.Equals(s3));

Console.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s4), s1 == s4, s1.Equals(s4));

//"=="  Type should be same

//" refrenceequals reference" of the object should be same.

//" Equals" values should be same.

Console.ReadLine();

Output

True True True

False True True

False False True