I got hit by a strange "asymmetry" in C# that I do not really understand. See the following code:
using System;
using System.Diagnostics;
namespace EqualsExperiment
{
class Program
{
static void Main(string[] args)
{
object apple = "apple";
object orange = string.Format("{0}{1}", "ap", "ple");
Console.WriteLine("1");
Debug.Assert(apple.Equals(orange));
Console.WriteLine("2");
Debug.Assert(apple == orange);
Console.WriteLine("3");
}
}
}
It might be obvious for all you .NET gurus, but the 2nd assert fails.
In Java I have learnt that == is a synonym for something called Object.ReferenceEquals here. In C#, I thought that Object.operator== uses Object.Equals, which is virtual, so it is overriden in the System.String class.
Can someone explain, why does the 2nd assert fail in C#? Which of my assumptions are bad?
The
==
operator is not a synonym, it's an operator that is defined for different types.The
==
operator is defined for strings, and then it does actually use theEquals
method:However, in your code you are not using the operator on strings, you are using it on objects, so what you get is the
==
operator defined for objects, which usesReferenceEquals
to do the comparison.Which overload of the operator to use is decided at compile time, so it's the type of the variables that decide the overload, not the actual type of the objects that the variables point to.
Operators are defined as static methods, so they can't participate in polymorphism. So your second assert uses the definition of
==
forobject
(since your variables are declared asobject
), which only tests reference equality. If the variables were declared asstring
, the==
overload forstring
would have been used, and the second assert would have succeeded.