覆盖具有 ID 的对象的等于
对于具有唯一 ID
的对象来说,它是 Equals
(在 VB.NET 中)的最佳重写吗?
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If obj Is Nothing OrElse Not [GetType]().Equals(obj.GetType()) Then
Return False
End If
Dim otherMyObject As MyObject = DirectCast(obj, MyObject)
Return Me.Id = otherMyObject.Id
End Function
我从 MSDN 中获取了该示例,但不完全确定是否来自所有观点(包括性能)是更好的解决方案。
编辑:
与此版本进行比较怎么样:
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Dim otherMyObject As MyObject = TryCast(obj, MyObject)
If otherMyObject Is Nothing Then
Return False
Else
Return Me.Id = otherMyObject.Id
End If
End Function
Is it the best override for Equals
(in VB.NET) for an object having an unique ID
?
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If obj Is Nothing OrElse Not [GetType]().Equals(obj.GetType()) Then
Return False
End If
Dim otherMyObject As MyObject = DirectCast(obj, MyObject)
Return Me.Id = otherMyObject.Id
End Function
I took that example from the MSDN, but not entirely sure if from all points of view (including performance) is the better solution.
EDIT:
How about comparing with this version:
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Dim otherMyObject As MyObject = TryCast(obj, MyObject)
If otherMyObject Is Nothing Then
Return False
Else
Return Me.Id = otherMyObject.Id
End If
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于“唯一 ID”的含义。如果您可以保证内存中不会有任何其他具有相同 ID 的对象,则可以直接继承
Object
的功能。但是,如果您希望将两个具有相同类型和 ID 的对象视为相等,那么这看起来不错。不过,您还想重写GetHashCode
。您确实需要考虑什么将使用平等实现以及他们的期望。如果可以有两个不同的对象具有相同的 ID 但其他属性不同,那么它们“相等”意味着什么?这样做合适不合适?
例如,以“
IdEqualityComparer
”类型实现IEqualityComparer(Of T)
可能更有意义。这是表达平等关系有用的好方法,但不一定是“自然的、全面的”平等关系。It depends what you mean by "an unique ID". If you can guarantee that there won't be any other objects in memory with the same ID, you can just inherit the functionality from
Object
. If, however, you want to treat two objects of the same type and ID as being equal, then this looks fine. You'd want to overrideGetHashCode
as well though.You really need to consider what is going to use the equality implementation and what they're expecting. If there can be two distinct objects with the same ID but different other properties, what does it mean for those to be "equal"? Is that appropriate or not?
It may make more sense to implement
IEqualityComparer(Of T)
in something like an "IdEqualityComparer
" type, for example. That's a good way of expressing that an equality relation is useful but not necessarily a "natural, comprehensive" equality relation.这取决于您的应用程序中对“等于”的要求。具有相同
Id
属性的实例ClassA
是否始终“等于”具有相同Id
的另一个实例?如果实例 #1 已更改,并且某些属性与实例 #2 不同,但仍具有相同的Id
,该怎么办?他们还平等吗?在大多数定义中,这不会相等,但这完全取决于您自己的要求。It depends on your requirements for "Equals" in your application. Does an instance
ClassA
with the sameId
property always "Equal" another instance with the sameId
? What if instance #1 has been changed, and some of the properties are different than instance #2, but still have the sameId
. Are they still equal? In most definitions this would not be equal, but that's entirely up to your own requirements.