I have a question about Object.Equals
and Equals(object)
. My sample code is below:
class Program
{
static void Main(string[] args)
{
var sb1 = new StringBuilder("Food");
var sb2 = new StringBuilder("Food");
Console.WriteLine(sb1 == sb2);
Console.WriteLine(sb1.Equals(sb2));
Console.WriteLine(Object.Equals(sb1, sb2));
Console.WriteLine(Object.ReferenceEquals(sb1, sb2));
Console.ReadLine();
}
}
The output is:
False
True
False
False
But as far as I'm concerned Object.Equals(sb1, sb2)
internally calls sb1.Equals(sb2)
so why does it give two different results?
You are missing another test:
StringBuilder does not override
Equals(object)
, it overloads it with anotherEquals(StringBuilder)
.Object.Equals(object, object)
is callingEquals(object)
, so the result is false.