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:
Console.WriteLine(sb1.Equals((object)sb2)); // False!
StringBuilder does not override Equals(object), it overloads it with another Equals(StringBuilder).
Object.Equals(object, object) is calling Equals(object), so the result is false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With