重写 GetHashCode

发布于 2024-10-03 11:33:42 字数 169 浏览 2 评论 0原文

如您所知,GetHashCode 返回一个半唯一值,可用于标识集合中的对象实例。作为一个好的做法,建议重写此方法并实现您自己的方法。

我的问题是 - 在处理自定义对象时您是否会覆盖此方法?如果是这样,您使用什么算法来生成唯一 ID?

我正在考虑生成一个 GUID,然后从该标识符获取整数数据。

As you know, GetHashCode returns a semi-unique value that can be used to identify an object instance in a collection. As a good practice, it is recommended to override this method and implement your own.

My question is - do you override this method when working on custom objects? If so, what algorithm do you use to generate the unique ID?

I was thinking about generating a GUID and then getting integer data from that identificator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

善良天后 2024-10-10 11:33:42

如果您使用 resharper,它可以为您生成 GetHashCode()、Equals 和运算符方法体。

Alt+插入 访问此菜单。

http://www.jetbrains.com/resharper/webhelp/Code_Generation__Equality_Members.html

If you use resharper it can generate the GetHashCode(), Equals and operator method bodies for you.

Access this menu by pressing Alt+Insert.

http://www.jetbrains.com/resharper/webhelp/Code_Generation__Equality_Members.html

紫罗兰の梦幻 2024-10-10 11:33:42

当您重写 GetHashCode() 时,您还需要重写 Equals()operator==operator!= 。并且要非常小心地满足这些方法的所有要求。

这些指南位于 MSDN 上。最重要的引言:

在可变类型中重写运算符 == 不是一个好主意。

When you override GetHashCode() you also need to override Equals(), operator== and operator!= . And be very careful to meet all the requirements for those methods.

The guidelines are here on MSDN. Most important quote:

It is not a good idea to override operator == in mutable types.

国际总奸 2024-10-10 11:33:42

在我个人的使用中,我也只在重写 equals 方法时才进行重写。一般来说,我对我知道可能运行 LINQ to Objects 查询或其他比较操作的对象执行此操作。

如果是 LINQ to SQL 实体或 DTO 对象,我通常会返回主键值。无论您返回什么,如果不在本地存储该值,都可能会产生意外结果。

HTH。

In my personal usage, I only override when overriding equals method too. Generally, I do this for objects I know that I might run a LINQ to Objects query on, or some other comparison operation.

I usually return, if say a LINQ to SQL entity or DTO object, the primary key value. Whatever you return, if you don't store the value locally, it may produce an unexpected result.

HTH.

暮凉 2024-10-10 11:33:42

我通常会覆盖数据类(即值语义有意义的类)的哈希码和相等检查方法。看看这个问题以获得共同的实施。如果您确实覆盖哈希码,则覆盖等于。使用 GUID 是一个非常糟糕的想法,因为您希望两个不同实例但具有相同的对象具有相同的哈希码,并且 equals 返回 true。

I would normally override hashcode and equality checking methods for data classes (i.e. classes where the value semantics makes sense). Have a look at this question for a common implementation. If you do override hashcode override equals. Using a GUID is a pretty terrible idea because you want two objects which are different instances but have the same value to have the same hashcode and for equals to return true.

左耳近心 2024-10-10 11:33:42

如果您要覆盖 Equals,则只需覆盖 GetHashCode。默认的 GetHashCode 由运行时以与您想要的方式类似的方式实现 - 每个对象都有一个由运行时分配的隐藏字段。

如何覆盖 GetHashCode

实际上是你的IDE 应该为您执行此操作 - 当您键入“override GetHashCode”时,IDE 应该生成此样板代码。 Visual Studio 不会这样做,但 SharpDevelop 会这样做。

you only need to override GetHashCode if you are overriding Equals. The default GetHashCode is implemented by the runtime in a similar way you wanted to do it - every object has a hidden field assigned by the runtime.

How to override GetHashCode

Actually your IDE should do this for you - when you type "override GetHashCode" the IDE should generate this boilerplate code. Visual Studio does not do it but SharpDevelop does.

怀中猫帐中妖 2024-10-10 11:33:42

通常,我使用类的组件属性中聚合的 GetHashCode。例如

public class Test
{
  public string Text { get; set; }
  public int Age { get; set; }

  public override GetHashCode()
  {
    int result = 
      string.IsNullOrEmpty(Text) ? 0 : Text.GetHashCode()
      + Age.GetHashCode();

    return result;
  }
}

Generally I use the aggregated GetHashCode from the component properties of the class. E.g.

public class Test
{
  public string Text { get; set; }
  public int Age { get; set; }

  public override GetHashCode()
  {
    int result = 
      string.IsNullOrEmpty(Text) ? 0 : Text.GetHashCode()
      + Age.GetHashCode();

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