本文介绍了比较一个类的两个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个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 instanceHow should i do? override == ? use delegate ?I am sorry ,my english was poor.推荐答案 我一直试图找出同样的东西。 从我所看到的,如果你在课堂上覆盖==那么你应该 也会覆盖Equals,!=和GetHashCode。 因此它们都表现出相同的行为。 因为我可能想在一个案例中测试参考相等性并说 检查另一个案例中的ID属性case我想覆盖==到 检查ID是否相等但是从我的理解那不是个好主意 因为其他语言(VB)没有这个运算符。 您可能想要定义一个接口,对两个实例进行属性检查以确定它们是否相等。 对不起,我不能更果断,我希望看到别人的意见:) 干杯 - JBI have been trying to find out the same thing.From what I can see, if you overrride == in your class then you shouldalso 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 saycheck the ID property in another case I would like to override == tocheck for ID equality but from what I understand thats not a good ideaas other languages (VB) does not have this operator.You might want to define an interface that would do a property check onboth instances to determine whether they are equal.Sorry I cannot be more decisive, I would like to see others views :)Cheers--JB 这篇关于比较一个类的两个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 11:13