比较2自定义对象 - C# [英] Comparing 2 custom objects - C#

查看:106
本文介绍了比较2自定义对象 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写在愿意接受2个对象作为参数,并比较他们平等的基类的通用方法。

I need to write a generic method in the base class that would accept 2 objects as parameters and compares them for equality.

例如:

public abstract class BaseData
{

  public bool AreEqual(object O1, object O2)
  {
    //Need to implement this
  }
}

public class DataTypeOne : BaseData
{
  public string Name;
  public string Address;
}

public class DataTypeTwo : BaseData
{
  public int CustId;
  public string CustName;
}

AreEqual()方法将接受 DataTypeOne 2实例或 2实例DataTypeTwo

The AreEqual() method would accept 2 instances of DataTypeOne or 2 instances of DataTypeTwo.

我的猜测是,我需要使用反射?我可以使用LINQ如果能够更可读/简洁。

My guess is I need to use Reflection? I can use LINQ if it can be more readable/concise.

编辑: 我想实现这个方法在基类的原因是因为项目的限制。有大量的开发者工作的派生类。通过在基类中实现这一点,我想有1少的事情,让他们担心。

The reason I would like to implement this method in the base class is because of project restrictions. There are a large number of devs working on the derived classes. By implementing this in the base class, I am trying to have 1 less thing for them to worry about.

推荐答案

(假设你想要的是比较两个对象是否相等的各个领域。)

(Assuming that what you want is to compare all the fields of the two objects for equality.)

通常情况下,你就懒得使用反射对于这一点,你只是你自己比较每个领域。该 IEquatable< T> 已存在这方面的接口,你也可能要重写的Object.Equals()上问题的类型。例如:

Usually, you wouldn't bother using reflection for this, you'd just compare each field yourself. The IEquatable<T> interface exists for this purpose, and you also might want to override Object.Equals() on the types in question. For example:

public class DataTypeTwo : BaseData, IEquatable<DataTypeTwo>
{
    public int CustId;
    public string CustName;

    public override int GetHashCode()
    {
        return CustId ^ CustName.GetHashCode(); // or whatever
    }

    public override bool Equals(object other)
    {
        return this.Equals(other as DataTypeTwo);
    }

    public bool Equals(DataTypeTwo other)
    {
        return (other != null &&
                other.CustId == this.CustId &&
                other.CustName == this.CustName);
    }
}

另外,还要考虑您的类型是否有道理作为结构代替。值类型自动做一个现场逐场比较比较平等的。

Also, consider whether or not your type makes sense as a struct instead. Value types automatically compare equality by doing a field-by-field comparison.

请注意,通过覆盖等于,你基本上达到什么样的(在我看来),你想实现你的主人equals方法计划。也就是说,使用人 DataTypeTwo 将能够自然地测试相等,而不必知道什么特殊的API - 他们将只使用等于就像他们与其他的东西。

Note that by overriding Equals, you basically achieve what (it seems to me) you're trying to achieve with your "master equals method" scheme. That is, people using DataTypeTwo will be able to naturally test for equality without having to know anything special about your API -- they'll just use Equals like they would with other things.

编辑:多亏了贾里德提醒我关于 GetHash code 。您还需要重写它通过确保任意两个对象分别是平等也返回相同的哈希code维持在哈希表的正常外观的行为。

Thanks to Jared for reminding me about GetHashCode. You'll also want to override it to maintain normal-looking behavior in hashtables by making sure that any two objects which are "equal" also return the same hash code.

这篇关于比较2自定义对象 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆