覆盖具有 ID 的对象的等于

发布于 2024-12-09 16:35:51 字数 820 浏览 0 评论 0原文

对于具有唯一 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-12-16 16:35:51

这取决于“唯一 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 override GetHashCode 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.

相权↑美人 2024-12-16 16:35:51

这取决于您的应用程序中对“等于”的要求。具有相同 Id 属性的实例 ClassA 是否始终“等于”具有相同 Id 的另一个实例?如果实例 #1 已更改,并且某些属性与实例 #2 不同,但仍具有相同的 Id,该怎么办?他们还平等吗?在大多数定义中,这不会相等,但这完全取决于您自己的要求。

It depends on your requirements for "Equals" in your application. Does an instance ClassA with the same Id property always "Equal" another instance with the same Id? What if instance #1 has been changed, and some of the properties are different than instance #2, but still have the same Id. Are they still equal? In most definitions this would not be equal, but that's entirely up to your own requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文