简体   繁体   English

重载==运算符

[英]Overloading == operator

A derives directly from Object class and neither A or Object overload == operator, so why doesn't next code cause an error: A直接从Object类派生,既不是AObject重载==运算符,那么为什么下一个代码不会导致错误:

class Program
{
    static void Main(string[] args)
    {
        A a1 = new A();
        A a2 = new A();

        if (a1 == a2) ... ;
    }
}

class A { }

thanx 感谢名单

A derives directly from Object class and neither A or Object overload == operator, so why doesn't next code cause an error? A直接从Object类派生,既不是A或Object的重载==运算符,那么为什么下一个代码不会导致错误呢?

As with your other question, you seem to have some strange belief that whether an overloaded operator exists has any bearing on whether an operator can be meaningfully chosen. 与您的其他问题一样,您似乎有一些奇怪的信念,即是否存在重载运算符与是否可以有意义地选择运算符有关。 It does not . 它没有

Again, to resolve this situation overload resolution first attempts to determine if there is a user-defined operator defined on either of the operands. 同样,为了解决这种情况,重载决策首先尝试确定是否在任一操作数上定义了用户定义的运算符。 As you note, there is not. 如你所知,没有。

Overload resolution then falls back on the built-in operators. 然后,重载分辨率会回退到内置运算符。 As I mentioned in your other question, the built-in operators are the equality operators on int, uint, long, ulong, bool, char, float, double, decimal, object, string, all delegate types and all enum types, plus the lifted-to-nullable versions of all the value types. 正如我在你的另一个问题中提到的,内置运算符是int,uint,long,ulong,bool,char,float,double,decimal,object,string,所有委托类型和所有枚举类型的相等运算符,加上所有值类型的提升到可空的版本。

Given those operators we must now determine the applicable ones. 鉴于这些运营商我们现在必须确定适用的运营商。 There is no implicit conversion from "A" to any of the value types, to any of the nullable value types, to string, or to any delegate type. 没有从“A”到任何值类型,任何可空值类型,字符串或任何委托类型的隐式转换。

The only remaining applicable candidate is object. 唯一剩下的适用候选人是对象。

If overload resolution chooses the equality operator that compares two objects, additional constraints must be met. 如果重载决策选择了比较两个对象的相等运算符,则必须满足其他约束。 In particular, both operands must either be null or a reference type, or a type parameter not constrained to be a value type. 特别是,两个操作数必须为null或引用类型,或者不限制为值类型的类型参数。 That constraint is met. 满足了这种约束。 Also, if the two sides have types then the operand types must have some sort of compatibility relationship; 另外,如果双方都有类型,那么操作数类型必须具有某种兼容关系; you can't do "myString == myException" because there is no relationship between string and Exception. 你不能做“myString == myException”因为string和Exception之间没有关系。 There is a relationship between "A" and "A", namely, they are identical. “A”和“A”之间存在关系,即它们是相同的。

Therefore the reference equality operator is chosen, and the == means "compare these two object expressions by reference". 因此,选择引用相等运算符,==表示“通过引用比较这两个对象表达式”。

I am mystified as to why you believe having a user-defined == operator has anything to do with this, either in this question or your other question. 我很困惑,为什么你认为有一个用户定义的==运算符与此有关,无论是在这个问题还是你的其他问题。 The absence of such a method does not prevent the compiler from generating whatever code it likes for this expression. 缺少这样的方法并不妨碍编译器为此表达式生成它喜欢的任何代码。 Can you explain? 你可以解释吗?

Because by default, the == operator compares the references (memory locations) of the objects a1 and a2 . 因为默认情况下,==运算符会比较对象a1a2的引用(内存位置)。 And because they're different instances of class A , the expression a1 == a2 always evaluates to false in your example. 并且因为它们是AA不同实例,所以表达式a1 == a2在您的示例中始终计算为false。

Objects have a default implementation of the == operator that checks if they refer to the same object (reference comparison). 对象具有==运算符的默认实现,用于检查它们是否引用同一对象(引用比较)。 So there's no reason for it to be an error. 因此,没有理由将其作为错误。 The operator does have a meaning. 运营商确实有意义。

因为Object有一个比较引用的默认实现。

调用base的==运算符,说明为什么它没有给出任何错误。

By default, the operator == tests for reference equality by determining if two references indicate the same object, so reference types do not need to implement operator == in order to gain this functionality. 默认情况下,operator ==通过确定两个引用是否指示相同的对象来测试引用相等性,因此引用类型不需要实现operator ==以获得此功能。

From: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx 来自: http//msdn.microsoft.com/en-us/library/ms173147(v = vs.80).aspx

The important bit concerning your question being: 关于你的问题的重要一点是:

reference types do not need to implement operator == in order to gain this functionality 引用类型不需要实现operator ==以获得此功能

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

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