简体   繁体   English

覆盖Equals和GetHashCode-派生类中的默认实现

[英]Overriding Equals and GetHashCode - default implementation in derived class

I'm a little confused about the default behaviour of Equals and GetHashCode in C#. 我对C#中的Equals和GetHashCode的默认行为感到有些困惑。 Say I have two classes, one deriving from the other: 假设我有两堂课,一堂课衍生自另一堂课:

public abstract class Question
    {
        public string QuestionText
        {
            get;
            set;
        }

        public override bool Equals(object obj)
        {
            if (obj is Question)
            {
                Question q = (Question)obj;
                return this.QuestionText.Equals(q.QuestionText);
            }
            else
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            int hash = 13; 
            hash = (hash * 7) + this.QuestionText.GetHashCode(); 
            return hash; 
        }
 }

public class QuestionTrueFalse : Question
    {
        public bool CorrectAnswer
        {
            get;
            set;
        }

        public override bool Equals(object obj)
        {
            return base.Equals(q);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode(); 
        }
    } 

The derived class doesn't affect whether one item equals another, I still want that to be based simply upon the QuestionText property. 派生类不影响一项是否等于另一项,我仍然希望该项仅基于QuestionText属性。

Do I need to override Equals and GetHashCode to reference the base implementation, as I have done here, or is that the default behaviour? 我是否需要像这里所做的那样重写Equals和GetHashCode来引用基本实现,或者这是默认行为吗?

The base class behavior is inherited by the inheriting classes. 基类的行为由继承的类继承。 You don't need to explicitly override Equals and GetHashCode unless you want to change their behavior. 除非要更改其行为,否则无需显式覆盖EqualsGetHashCode

  1. You probably don't want this. 您可能不想要这个。 Why would you want to to have separate but Equal instances of a question object? 您为什么要拥有一个问题对象的单独但相等的实例?

  2. You will at least have to add operator== and operator!= to prevent nasty suprises. 您至少必须添加operator==operator!=才能防止令人讨厌的意外发生。

  3. But no, you don't need to override in QuestionTrueFalse to call the base implementation. 但是,不,您不需要在QuestionTrueFalse重写即可调用基本实现。 That is provided for. 这是规定的。

The standard example (can't come up with a more PC one): 标准示例(无法提供更多PC):

Q1: "Do you still beat your wife?" 问题1:“您还打败妻子吗?” { true, false } { 真假 }
Q2: "Do you still beat your wife?" 问题2:“您还打败妻子吗?” { true, false, N/A } {正确,错误,不适用}

Are they really the same? 他们真的一样吗?

Having a class derived from a class which cares about Equals and GetHashCode is not that simple. 从关心Equals和GetHashCode的类派生一个类并不是那么简单。 There are many considerations omitted by just letting the base class do the job. 仅让基类完成任务就可以忽略许多注意事项。

You can refer to this article for deeper analysis how purpose of Equals and GetHashCode methods changes once the class is derived: How to Override Equals and GetHashCode Methods in Base and Derived Classes 您可以参考本文进行更深入的分析,一旦派生该类,Equals和GetHashCode方法的用途将如何改变: 如何在基类和派生类中重写Equals和GetHashCode方法

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

相关问题 static Object.Equals方法,GetHashCode的默认实现和Dictionary类 - static Object.Equals method, default implementation of GetHashCode and the Dictionary class 没有状态的派生类中的 Equals/GetHashCode 覆盖警告 - Equals/GetHashCode override warning in derived class with no state GetHashCode等于C#中的类的实现 - GetHashCode Equals implementation for a class in C# GetHashCode 实现使用元组派生 class - GetHashCode implementation using tuples for derived class 通过覆盖GetHashCode和Equals从IEqualityComparer &lt;&gt;派生外部类的优点 - Advantage of deriving external class from IEqualityComparer<> over overriding GetHashCode and Equals 覆盖类上的GetHashCode和Equals,因为它在字典中使用 - Overriding GetHashCode and Equals on a class because it's used in a dictionary 实现EqualityCompare与覆盖GetHashCode和Equals - Implementing EqualityCompare vs overriding GetHashCode and Equals 覆盖一个类型的Equals和GetHashCode,它有&#39;dibs&#39;? - Overriding the Equals and GetHashCode of a type, which has 'dibs'? C#,在重写GetHashCode和Equals时应考虑哪些类字段/成员? - C#, Which class fields/members should be considered when overriding GetHashCode and Equals? 覆盖派生类中的默认值(c#) - Overriding default value in derived class (c#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM