简体   繁体   English

重载等于,这是错误的吗?

[英]overload Equals, is this wrong?

Reading some piece of code and I keep seeing this : 阅读一些代码,我一直看到:

public override bool Equals (object obj)
{ 
    if (obj == null || this.GetType ().Equals (obj.GetType())) return false; 
    //compare code...
}

Shouldn't it be like this (note the !): 它不应该像这样(注意!):

public override bool Equals (object obj)
{ 
    if (obj == null || !this.GetType ().Equals (obj.GetType())) return false; 
    //compare code...
}

Or does the equals perform differently in this case? 还是在这种情况下平等表现不同?

That looks like a bug. 这看起来像一个bug。 Returning false when the types are the same is certainly not the intended behaviour. 类型相同时返回false当然不是预期的行为。

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

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