【问题标题】:How to check whether all two objects' properties are equal, including derived ones?如何检查所有两个对象的属性是否相等,包括派生的?
【发布时间】:2017-11-26 16:46:49
【问题描述】:

假设我有这三个类:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int IdNumber { get; set; }
    public string Address { get; set; }

    // Constructor and methods.
}

class Employee : Person
{
    public byte SalaryPerHour { get; set; }
    public byte HoursPerMonth { get; set; }

    // Constructor and methods.
}

class Seller : Employee
{
    public short SalesGoal { get; set; }
    public bool MetSaleGoleLastYear { get; set; }

    // Constructor and methods.
}

我会像这样实现IEquatable<T>

public bool Equals(Person other)
{
    if (other == null) return false;
    return FirstName == other.FirstName
        && LastName == other.LastName
        && IdNumber == other.IdNumber
        && Address == other.Address;
}

public bool Equals(Employee other)
{
    if (other == null) return false;
    return FirstName == other.FirstName
        && LastName == other.LastName
        && IdNumber == other.IdNumber
        && Address == other.Address
        && SalaryPerHour == other.SalaryPerHour
        && HoursPerMonth == other.HoursPerMonth;
}

public bool Equals(Seller other)
{
    if (other == null) return false;
    return FirstName == other.FirstName
        && LastName == other.LastName
        && IdNumber == other.IdNumber
        && Address == other.Address
        && SalaryPerHour == other.SalaryPerHour
        && HoursPerMonth == other.HoursPerMonth
        && SalesGoal == other.SalesGoal
        && MetSaleGoleLastYear == other.MetSaleGoleLastYear;
}

现在,如您所见,一个类在继承链中的位置越多,我需要检查的属性就越多。例如,如果我从其他人编写的类继承,我还需要查看类代码以查找其所有属性,因此我可以使用它们来检查值是否相等。对我来说这听起来很奇怪。难道没有更好的方法吗?

【问题讨论】:

    标签: c# equals


    【解决方案1】:

    除了 John Wu 的回答,这里是更正后的完整解决方案:

    public bool Equals(Person other)
    {
        if (other == null) return false;
        return FirstName == other.FirstName
            && LastName == other.LastName
            && IdNumber == other.IdNumber
            && Address == other.Address;
    }
    
    public bool Equals(Employee other)
    {
        if (other == null) return false;
        return base.Equals(other)
            && SalaryPerHour == other.SalaryPerHour // ; removed
            && HoursPerMonth == other.HoursPerMonth;
    }
    
    public bool Equals(Seller other)
    {
        if (other == null) return false;
        return base.Equals(other)
            && SalesGoal == other.SalesGoal // SalaryPerHour;
            && MetSaleGoleLastYear == other.MetSaleGoleLastYear; //HoursPerMonth;
    }
    

    【讨论】:

      【解决方案2】:

      使用基地。短得多。

      public bool Equals(Seller other)
      {
          if (other == null) return false;
          return base.Equals(other)
          && SalesGoal == other.SalaryPerHour;
          && MetSaleGoleLastYear == other.HoursPerMonth;
      }
      

      【讨论】:

      • 这个,也不是原始代码,处理other != null但变量之一的情况,例如other.FirstName, == null。这种情况可能吗?
      • 如果other.FirstName == nullEquals 将返回false,除非this.FirstName == null 也是。我认为这是正确的行为。
      • 你说得对,我在想当你尝试访问空对象的变量时抛出的异常,但other != null 足以检查这一点。
      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多