比较一个类的两个实例 [英] Compare two instance of a class

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

问题描述




我有一个C#类和两个类的实例;


该类有一些属性。


我想比较两个实例的属性值


我该怎么办?覆盖==?使用代表?


对不起,我的英语很差。

Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.

推荐答案

bengamin写道:
bengamin wrote:


我有一个C#类和两个类的实例;

这个类有一些属性。

我想比较两个实例的属性值

我该怎么办?覆盖==?使用代表?

对不起,我的英语很差。
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.




我一直试图找出同样的东西。

从我所看到的,如果你在课堂上覆盖==那么你应该

也会覆盖Equals,!=和GetHashCode。

因此它们都表现出相同的行为。


因为我可能想在一个案例中测试参考相等性并说

检查另一个案例中的ID属性case我想覆盖==到

检查ID是否相等但是从我的理解那不是个好主意

因为其他语言(VB)没有这个运算符。


您可能想要定义一个接口,对两个实例进行属性检查以确定它们是否相等。


对不起,我不能更果断,我希望看到别人的意见:)


干杯


-

JB



I have been trying to find out the same thing.
From what I can see, if you overrride == in your class then you should
also override Equals, != and GetHashCode.
So that they all exhibit the same behaviour.

Since I might want to test for reference equality in one case and say
check the ID property in another case I would like to override == to
check for ID equality but from what I understand thats not a good idea
as other languages (VB) does not have this operator.

You might want to define an interface that would do a property check on
both instances to determine whether they are equal.

Sorry I cannot be more decisive, I would like to see others views :)

Cheers

--
JB


推荐pra ctice是那个运算符==和方法

" Object.Equals(object)"应该表现相同。

如果你重写Equal你也应该覆盖GetHashCode。


你需要记住你是否需要比较2

引用(这是默认行为)即引用相同的

实例。


在大多数情况下我认为最好实现一个特定的方法来比较

引用类型实例 - 值比较通常是业务/应用程序

特定功能,如果某些开发人员可能会误导你

覆盖默认语言功能。


这是MSDN上的一篇有用的文章:
http://msdn.microsoft.com/library/ de ... lsoperator.asp


理查德。


" bengamin" < SA *** @ langchao.com>在消息中写道

news:ea ************** @ tk2msftngp13.phx.gbl ...
Recommended practice is that operator "==" and method
"Object.Equals(object)" should behave the same.
If you override Equal you should also override GetHashCode.

You need to bear in mind whether or not you will ever need to compare 2
references (which is the default behaviour) i.e. that refer to the same
instance.

In most cases I think it is better to implement a specific method to compare
reference type instances - value comparison is often a business/application
specific function, and it may be misleading to some developers if you
override default language functionality.

Here''s a useful article on MSDN:
http://msdn.microsoft.com/library/de...lsoperator.asp

Richard.

"bengamin" <sa***@langchao.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...

我有一个C#类和两个类的实例;

这个类有一些属性。

我想比较两个实例的属性值

我该怎么办?覆盖==?使用代表?

我很抱歉,我的英语很差。
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.



抱歉,我已经得到了好奇,因为这些回复。我好像不能理解这个问题吗?


我认为他的意思只是如果你让我们说'以下

等级:


A级

{

private int _i;


public int i

{

get

{return this._i; }

设定

{this._i = value; }

}

}


和A a1 = new A(); A2 =新的A();

然后a1.i == a2.i只要它们具有相同的值就会返回true?那个

是个问题吧?为什么你需要让运营商超载

那个?

" richlm" < RI ***** @ h0tmai1.com>在消息中写道

新闻:OS ************** @ TK2MSFTNGP10.phx.gbl ...
Sorry but I''ve gotten curious because of these replies. I don''t seem to
understand the question maybe?

I thought what he meant was just that if you have let''s say, the following
class:

class A
{
private int _i;

public int i
{
get
{ return this._i; }
set
{ this._i = value; }
}
}

and A a1 = new A(); A a2 = new A();
then a1.i == a2.i would simply return true if they have the same value? That
was the question right? Why would you need to overload the operator for
that?
"richlm" <ri*****@h0tmai1.com> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...
推荐做法是那个算子" =="和方法
Object.Equals(object)应该表现相同。
如果你重写Equal你也应该覆盖GetHashCode。

你需要记住你是否需要比较2
引用(是默认行为)即引用相同的
实例。

在大多数情况下,我认为最好将
比较引用类型实例的特定方法实现 - 值比较通常是
业务/应用程序特定功能,如果你覆盖默认语言功能,它可能会误导一些开发人员。

这是MSDN上的一篇有用的文章:
http://msdn.microsoft.com/library/de...lsoperator.asp
理查德。

bengamin < SA *** @ langchao.com>在消息中写道
新闻:ea ************** @ tk2msftngp13.phx.gbl ...
Recommended practice is that operator "==" and method
"Object.Equals(object)" should behave the same.
If you override Equal you should also override GetHashCode.

You need to bear in mind whether or not you will ever need to compare 2
references (which is the default behaviour) i.e. that refer to the same
instance.

In most cases I think it is better to implement a specific method to compare reference type instances - value comparison is often a business/application specific function, and it may be misleading to some developers if you
override default language functionality.

Here''s a useful article on MSDN:
http://msdn.microsoft.com/library/de...lsoperator.asp
Richard.

"bengamin" <sa***@langchao.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...

我有一个C#类和两个类的实例;

这个类有一些属性。

我想比较两个实例的属性值

我该怎么办?覆盖==?使用代表?

对不起,我的英语很差。
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.




这篇关于比较一个类的两个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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