简体   繁体   English

Object.Equals是虚拟的,但是Object.operator ==不在C#中使用它吗?

[英]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. 我被我不太了解的C#中的一个奇怪的“不对称”击中。 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. 对于您所有的.NET专家而言,这可能是显而易见的,但是第二个断言失败。

In Java I have learnt that == is a synonym for something called Object.ReferenceEquals here. 在Java中,我了解到==是这里称为Object.ReferenceEquals的同义词。 In C#, I thought that Object.operator== uses Object.Equals, which is virtual, so it is overriden in the System.String class. 在C#中,我认为Object.operator ==使用的是Object.Equals,它是虚拟的,因此在System.String类中将其重写。

Can someone explain, why does the 2nd assert fail in C#? 谁能解释,为什么第二个断言在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: 为字符串定义了==运算符,然后实际上使用了Equals方法:

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. 但是,在代码中,您没有在字符串上使用运算符,而是在对象上使用了运算符,因此得到的是为对象定义的==运算符,该运算符使用ReferenceEquals进行比较。

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. 因此,您的第二个断言为object使用==的定义(因为您的变量被声明为object ),该定义仅测试引用的相等性。 If the variables were declared as string , the == overload for string would have been used, and the second assert would have succeeded. 如果变量声明为string ,则==超载的string会被使用,而第二断言会成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# “object.equals”中使用“或”(|)运算符 - How do I use “or” (|) operator in C# “object.equals” C#中的Object.Equals澄清? - Object.Equals clarification in C#? C#:静态object.Equals如何检查是否相等? - C#: How does the static object.Equals check for equality? C#operator ==,StringBuilder.Equals,Object.Equals和Object.ReferenceEquals之间的差异 - C# Differences between operator ==, StringBuilder.Equals, Object.Equals and Object.ReferenceEquals 带有多个或条件的 C# Object.Equals() - C# Object.Equals() with multiple or conditions C#运算符重载:Object.Equals(object o)和Object.GetHashCode() - C# operator overloading: Object.Equals(object o) & Object.GetHashCode() 有效的C#:覆盖Object.Equals(),不管是不是? - Effective C#: Overriding Object.Equals(), yay or nay? 警告:对象定义运算符 == 或运算符 != 但不会覆盖 Object.Equals(object o) - Warning: Object defines operator == or operator != but does not override Object.Equals(object o) c#中Object.Equals(object,object)和Object.ReferenceEquals(object,object)之间的区别 - difference between Object.Equals(object,object) and Object.ReferenceEquals(object,object) in c# Object.Equals的奇怪实现 - Strange implementation of Object.Equals
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM