简体   繁体   中英

Object.Equals is virtual, but Object.operator== does not use it in C#?

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 the Equals method:

public static bool operator ==(string a, string b) {
  return Equals(a, b);
}

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 uses ReferenceEquals 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 == for object (since your variables are declared as object ), which only tests reference equality. If the variables were declared as string , the == overload for string would have been used, and the second assert would have succeeded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM