当我比较一个对象(类型)时,它是否使用特定类的 IEquatable?
我的方法接收两个参数,均为 Object
类型。它们具有相同的类型,均实现 IEquatable
。
我的问题是:当我这样做时: param1 == param2
框架是否使用特定类的 IEquatable
运算符覆盖进行比较,还是使用 object.Equals< /code> 只是比较两个对象的内存指针?
哪种方法最好?是否具有泛型和派生约束?
My method receives two parameters, both of Object
type. They have the same type, that implements IEquatable
.
My question is: when I do: param1 == param2
does the framework compare using the IEquatable
operator override of specific class or does it uses the object.Equals
that just compares the memory pointer of two objects?
Which is the best way to do this? Is with generics and derivation constraints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,两者都没有。默认情况下,
==
运算符将测试引用相等性,无论您的Equals
方法的重写行为如何(如果您已重写它,那么您当然应该拥有它)实现了IEquatable
)。也就是说,如果您的变量类型为
object
但您想使用自己的自定义相等比较,请使用Equals(x, y)
而不是x == y
。然后,即使您已经实现了
IEquatable
,也请务必仍然重写object.Equals
,如下所示:虽然您确实 < em>还可以为您的类型重载
==
和!=
运算符,如果您引用了该类型的对象,那么这不会完成任何事情只是object
变量,如下所示:上面的内容不会像您预期的那样工作(如果您已经重载了
==
并希望使用它),因为 < code>== 重载必须在编译时解决;由于x
和y
被输入为任意对象,C# 编译器将选择object
类型的==
运算符,同样,它只是测试引用相等性。更新:现在,如果您的变量被键入为定义它的类,您可以确保使用
==
运算符或者 更派生的类型。例如,给定以下类型:上面的
AComparer.CompareEqual
方法将使用重载的==
运算符来处理任何类型T
源自A
。要记住的关键一点是
==
是静态,这意味着它的重载解析是在编译时执行的,而不是在运行时使用 vtable 执行的(除非您'正在使用dynamic
,但这完全是另一回事)。因此,每当您在代码中使用==
运算符并且希望重载解析为您的自定义类型时,请注意这一点。Actually, it does neither. The
==
operator by default will test for reference equality, regardless of the overridden behavior of yourEquals
method (if you've overridden it, which you certainly should have if you implementedIEquatable<T>
).That is to say, if your variables are typed as
object
but you want to use your own custom equality comparison, useEquals(x, y)
rather thanx == y
.Then, even if you've implemented
IEquatable<T>
, be sure to still overrideobject.Equals
, like this:While you certainly can also overload the
==
and!=
operators for your type, this won't accomplish anything if you have references to objects of this type that are simplyobject
variables, like this:The above won't work as you might expect (if you've overloaded
==
and expect that to be used) because the==
overload has to be resolved at compile-time; sincex
andy
are typed as arbitrary objects, the C# compiler will pick theobject
type's==
operator, which, again, just tests for reference equality.Update: Now, you can ensure your
==
operator is used if your variables are typed as the class wherein you defined it or a more derived type. For example, given the following types:The
AComparer<T>.CompareEqual
method above will use your overloaded==
operator for any typeT
deriving fromA
.The key thing to remember is that
==
is static, which means its overload resolution gets performed at compile-time, not at run-time using a vtable (unless you're usingdynamic
, but that's a whole other beast). So just be aware of that whenever you're using the==
operator in code and you want the overload to resolve to that of your custom type.您也必须重写 Equals,甚至 GetHashCode():看看这里:
http://blogs.msdn.com/b/jaredpar/archive/2009/01/15/if-you-implement-iequatable-t-you-still-must -覆盖-object-s-equals-and-gethashcode.aspx
You must override Equals too, and even GetHashCode(): have a look here:
http://blogs.msdn.com/b/jaredpar/archive/2009/01/15/if-you-implement-iequatable-t-you-still-must-override-object-s-equals-and-gethashcode.aspx
如果您的方法的参数指定为
object
,则执行param1 == param2
仅执行引用相等,因为==
运算符不是多态的。If your method's parameters are specified as
object
then doingparam1 == param2
just performs reference equality, since the==
operator isn't polymorphic.