The Object.Equals method tests whether the CONTENT of two objects are the same irrespective of whether the two objects are referring to the same memory location on the heap or not
Hence the below code snippet:
StringBuilder foo = new StringBuilder();
foo.Append("lol");
StringBuilder bar= new StringBuilder();
bar.Append("lol");
foo.Equals(bar)
will always return true since both foo and bar have the CONTENT as "lol"
However the following will return false:
Object.ReferenceEquals(foo,bar)
since foo and bar are two SEPARATE objects on the heap and hence their memory references are not the same.
The exception to the above is in case of strings.
Consider the following code snippet:
string
s1 = "Hello";
string s2 = "Hello";
Console.WriteLine(s1.Equals(s2)); // this returns true since both s1 and s2 contain the string 'Hello'
Console.WriteLine(Object.ReferenceEquals(s1, s2)); // this also returns true!!
Here the Object.ReferenceEquals returns true since although s1 and s2 are two different string objects; .NET CLR handles strings in a special way and according to my understanding uses string pooling wherein two strings that have the same content will point to the same memory location.
Let me know if there is any misunderstanding on my side with respect to the above statement