如何比较常见类的对象 [英] How can I compare the objects of common classes

查看:95
本文介绍了如何比较常见类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我创建了像这样的普通类的对象



Hi,

I created objects of common classes like this

public static CANBusDetails CANBusRedDetails = new CANBusDetails();
       public static CANBusDetails CANBusYellowDetails = new CANBusDetails();
       public static CANBusDetails CANBusGreenDetails = new CANBusDetails();
       public static CANBusDetails CANBusOrangeDetails = new CANBusDetails();





现在我使用以下代码填充公共类的对象与distic MsgIDs





Now I filled the the objects of common classes using following code with distict MsgIDs

private void ReadLogFile()
       {
// Copying Distinct CANMessageIds
           if (CANBusRedDetails != null)
               CANBusRedDetails.CANBusMsgIDList = ReturnDistinctCANMsgIds(Buses.CANBusRed, CANMsgIdList);

           if (CANBusYellowDetails != null)
               CANBusYellowDetails.CANBusMsgIDList = ReturnDistinctCANMsgIds(Buses.CANBusYellow, CANMsgIdList);

           if (CANBusGreenDetails != null)
               CANBusGreenDetails.CANBusMsgIDList = ReturnDistinctCANMsgIds(Buses.CANBusGreen, CANMsgIdList);

           if (CANBusOrangeDetails != null)
               CANBusOrangeDetails.CANBusMsgIDList = ReturnDistinctCANMsgIds(Buses.CANBusOrange, CANMsgIdList);
}







private List<string> ReturnDistinctCANMsgIds(Buses bus, List<MsgIdTimeStampMap> CANMsgIdList)
        {
            return (from MsgIdTimeStampMap busIdMap in CANMsgIdList
                    where busIdMap.Bus == bus
                    select busIdMap.MsgId).Distinct().ToList();
        }







现在,我如何将公共类的每个对象与对象进行比较在其他班级



谢谢

John




Now ,how can I compare each object of common class with the objects in other class

Thanks
John

推荐答案

你需要覆盖你的班级CANBusDetails的Equals和GetHashCode。查看此链接以获取更多信息和示例:



http://msdn.microsoft.com/en-us/library/vstudio/336aedhh%28v=vs.100%29.aspx [ ^ ]



祝你好运!
You need to override the Equals and GetHashCode of your class CANBusDetails. Check out this link for more info and an example:

http://msdn.microsoft.com/en-us/library/vstudio/336aedhh%28v=vs.100%29.aspx[^]

Good luck!


为了通过 value 分析对象相等性,你输入一个非常有趣的域名在C#中。



实现对象值比较的完整monte涉及使用IComparable,IComparer和IEquatable等接口,和/或覆盖以下操作符: ==等于,GetHashCode等等。是的,它很复杂,C#的新手经常在努力实现它。



参考:[ ^ ],[ ^ ],Jon Skeet(大师的大师)分析:[ ^ ]。



然而,有一种相对简单的方法(是的,不像强大的,如同更复杂的技术一样全功能)来证明原理。



考虑以下类定义:
For analyzing object equality by value, you enter a very interesting domain within C#.

The "full monte" in implementing value comparison of objects involves using interfaces like IComparable, IComparer, and IEquatable, and/or over-riding operators like: "==" "Equals," "GetHashCode," etc. Yes, it's complex, and newcomers to C# often struggle with getting it right.

Ref: [^], [^], Jon Skeet's (the gurus' guru) analysis: [^].

However, there is a relatively simple way (yes, not as robust, as full-featured as the more complex techniques) to demonstrate the principle.

Consider these Class definitions:
public class Class1
{
    public int Xint;
    public int Yint;

    public string SomeString;

    public Class1(int x, int y, string somestring)
    {
        Xint = x;
        Yint = y;
        SomeString = somestring;
    }

    private bool IsClass1Equals(Class1 i2)
    {
        return
            this.Xint == i2.Xint
            && this.Yint == i2.Yint
            && this.SomeString == i2.SomeString;
    }
}

public class Class2 : Class1
{
    public Class2(int x, int y, string somestring): base(x, y, somestring)
    {
    }
}

public class Class3 : Class1
{
    public Class3(int x, int y, string somestring): base(x, y, somestring)
    {
    }
}

你会注意到基类Class1实现了一个方法'IsClass1Equals,它比较了c的方法参数中某些Class1实例中每个字段的值Class1的urrent实例。



以下是测试方法:

You'll note that the base class, Class1, implements a method, 'IsClass1Equals, that compares the value of every field in some instance of Class1 in the parameter of the method to the current instance of Class1.

Here's how you might test this:

Class1 c1 = new Class1(100, 200, "a string");
           
Class1 c2 = new Class1(100, 200, "a string");

Class2 c3 = new Class2(200, 200, "another string");

Class3 c4 = new Class3(200, 200, "another string");

Class3 c5 = new Class3(200, 100, "another string");

Console.Write("c1 c2 values equal: ");
Console.WriteLine(c1.IsClass1Equals(c2).ToString());

Console.Write("c3 c4 values equal: ");
Console.WriteLine(c3.IsClass1Equals(c4).ToString());

Console.Write("c5 c4 values equal: ");
Console.WriteLine(c5.IsClass1Equals(c4).ToString());

在这里调用比较方法Equals是合法的,不使用'override,或者'新。但是,我认为最好在它们可能复制.NET名称时始终区分方法名称。



另请注意,没有问题(不需要转换类型)传递一个参数,该参数是从Class1继承的类的实例到Class1的IsClass1Equals方法,其中它被接收为类型Class1。

It would have been "legal" to call the comparison method "Equals" here, without using 'override, or 'new. However, I think it best to always distinguish method names any time they possibly duplicate .NET names.

Note also that there is no problem (no need for conversion of Type) passing in a parameter which is an instance of a class that inherits from Class1 to the IsClass1Equals method of Class1, where it is "received" as Type Class1.


这篇关于如何比较常见类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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