C# 如何比较用户创建的类的所有元素

C# 如何比较用户创建的类的所有元素,c#,C#,我有一个类定义为: public class TableInfo { public int item_id { get; set:} public string item_name { get; set;} // plus several more } 我创建了这个类的两个实例,其中填充了不同的信息,并希望对它们进行比较。我一直在用艰难的方式: if(table1[index1].item_id == table2[index2].item_id) //repo

我有一个类定义为:

public class TableInfo
{
    public int item_id { get; set:}
    public string item_name { get; set;}
    // plus several more
}
我创建了这个类的两个实例,其中填充了不同的信息,并希望对它们进行比较。我一直在用艰难的方式:

if(table1[index1].item_id == table2[index2].item_id)
    //report passed
else
    //report failed
然后对类中的每个元素再次执行该操作

if(table1[index1].item_name == table2[index2].item_name)
等等


有没有更好的方法来处理这个问题,这样我就不必对每个元素进行唯一的比较了。在我看来,foreach可以做到这一点,但我不确定如何获取属性列表并遍历它们。

您可以实现类的相等比较,然后使用
Equals进行比较:

public class TableInfo
{
    public int item_id { get; set;}
    public string item_name { get; set;}

    public override bool Equals(object obj)
    {
        if(obj == null)
            return false;
        if (ReferenceEquals(obj, this))
            return true;
        if (obj.GetType() != this.GetType())
            return false;
        var rhs = obj as TableInfo;
        return item_id == rhs.item_id && item_name == rhs.item_name;
    }

    public override int GetHashCode()
    {
        return item_id.GetHashCode() ^ item_name.GetHashCode();
    }

    // Additionally you can overload == and != operators:
    public static bool operator ==(TableInfo x, TableInfo y)
    {
        return object.Equals(x, y);
    }

    public static bool operator !=(TableInfo x, TableInfo y)
    {
        return !object.Equals(x, y);
    }
}
然后就不用了

if(table1[index1].item_id == table2[index2].item_id)
你可以用

if(table1[index1].Equals(table2[index2]))
if(table1[index1] == table2[index2])
或者,如果运算符重载,则可以使用

if(table1[index1].Equals(table2[index2]))
if(table1[index1] == table2[index2])

您可以实现类的相等比较,然后使用
Equals
进行比较:

public class TableInfo
{
    public int item_id { get; set;}
    public string item_name { get; set;}

    public override bool Equals(object obj)
    {
        if(obj == null)
            return false;
        if (ReferenceEquals(obj, this))
            return true;
        if (obj.GetType() != this.GetType())
            return false;
        var rhs = obj as TableInfo;
        return item_id == rhs.item_id && item_name == rhs.item_name;
    }

    public override int GetHashCode()
    {
        return item_id.GetHashCode() ^ item_name.GetHashCode();
    }

    // Additionally you can overload == and != operators:
    public static bool operator ==(TableInfo x, TableInfo y)
    {
        return object.Equals(x, y);
    }

    public static bool operator !=(TableInfo x, TableInfo y)
    {
        return !object.Equals(x, y);
    }
}
然后就不用了

if(table1[index1].item_id == table2[index2].item_id)
你可以用

if(table1[index1].Equals(table2[index2]))
if(table1[index1] == table2[index2])
或者,如果运算符重载,则可以使用

if(table1[index1].Equals(table2[index2]))
if(table1[index1] == table2[index2])

你可以用反射来解决它 只是展示另一种方法,但是实现平等比较会更干净

此代码段将仅比较公共属性


你可以用反射来解决它 只是展示另一种方法,但是实现平等比较会更干净

此代码段将仅比较公共属性


您可能希望在TableInfo类上实现IComparable。下面是一些关于实现IComparable的链接


您可能需要在TableInfo类上实现IComparable。下面是一些关于实现IComparable的链接


如果只是比较两个项目,可以使用Type.GetType().GetProperties()方法,该方法返回对象中所有公共属性的数组。从这里,您可以使用foreach循环迭代类型上的每个属性并比较字段


如果只是比较两个项目,可以使用Type.GetType().GetProperties()方法,该方法返回对象中所有公共属性的数组。从这里,您可以使用foreach循环迭代类型上的每个属性并比较字段


当不只是使用
&
运算符加入多重比较时?您可以只实现
等于(TableInfo)
和/或重载
=
运算符。当不只是使用
&
运算符加入多重比较时?您可以只实现
等于(TableInfo)
和/或重载
=
运算符。在这种情况下,我将如何使用该运算符?你的建议听起来像是我想要实现的。我不能完全理解正确的语法。在这种情况下,我应该如何使用它呢?你的建议听起来像是我想要实现的。我不能完全正确地理解语法。