如果我跳过实现GetHashCode会发生什么 [英] what happens if I skip implementing GetHashCode

查看:90
本文介绍了如果我跳过实现GetHashCode会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


这里我有一个卡类,有几种方法。


现在我的问题我不是真的完全了解这个

GetHashCode的用途是什么。

这就是我再次提起它的原因。
在下面这个简单的例子中如果我在这个Card类中跳过这个GetHashCode,那么Card类会怎么样?

是否有可能编写一些简单的测试程序来证明这个

GetHashCode会阻止

错误或问题发生吗?


类卡

{

//等级和套装在这里定义

公共静态布尔运算符==(卡片卡1,卡片卡2)

{

返回卡片1。西装== card2.suit)&& (card1.rank == card2.rank);

}


公共覆盖bool Equals(对象卡)

{

返回此==(卡)卡;

}


公共覆盖int HetHashCode()

{

返回13 *(int)rank +(int)suit;

}

....

....

}


// Tony

Hello!

Here I have a Card class with several methods.

Now to my question I don''t really fully understand what purpose this
GetHashCode is used for.
That''s why I bring it up again.
In this simple example below with the Card class what would happen if I skip
this GetHashCode in this Card class?
Is it possible to write some simple test program to prove that this
GetHashCode will prevent
error or problem to occur?

class Card
{
//rank and suit are defined here
public static bool operator==(Card card1, Card card2)
{
return card1.suit == card2.suit) && (card1.rank==card2.rank);
}

public override bool Equals(object card)
{
return this == (Card) card;
}

public override int HetHashCode()
{
return 13*(int)rank + (int)suit;
}
....
....
}

//Tony

推荐答案

2008年6月13日星期五00:01:31 -0700,Tony< jo ***************** @ telia.com>

写道:
On Fri, 13 Jun 2008 00:01:31 -0700, Tony <jo*****************@telia.com>
wrote:

[...]

现在我的问题我还没完全明白这是什么目的

GetHashCode用于。

这就是我再次提起它的原因。
在下面这个简单的例子中,如果我

跳过

这个Card类中的GetHashCode?
[...]
Now to my question I don''t really fully understand what purpose this
GetHashCode is used for.
That''s why I bring it up again.
In this simple example below with the Card class what would happen if I
skip
this GetHashCode in this Card class?



然后哈希表实现会将两个可能与
比较的实例视为相等,就好像它们不相等一样。同样地,

可能会使用哈希码。


如果你从不使用这个类的实例作为哈希表中的键或者

其他任何关心哈希码的东西,都没关系。


如果你决定继续实施GetHashCode()(和恕我直言,为什么

不是吗?),你应该拼写正确的方法名称。 :)


如果您需要有关哈希码如何使用的更多信息,我会建议一个关于该主题的好的计算机科学文本。一个新闻组不是最好的方式来教育自己这样的事情,这是一个相对高级的话题

值得多于一个或几个新闻组帖子。


Pete

Then hash table implementations will treat two instances that might have
compared as equal as if they are not equal. Likewise anything else that
might use the hash code.

If you never use instances of this class as keys in a hash table or with
anything else that cares about the hash code, it won''t matter.

If you do decide to go ahead and implement GetHashCode() (and IMHO, why
not?), you should probably spell the name of the method correctly. :)

If you need more information about how hash codes are used, I''d suggest a
good computer science text on the subject. A newsgroup isn''t the best way
to educate oneself on such things, it being a relatively advanced topic
deserving of much more than one or a few newsgroup posts.

Pete


你好!!


现在我们假设我存储实例卡片作为某种哈希的关键

表或类似的东西。


根据你的意思,哈希表实现会处理两个实例

可能与
比较相等,好像它们不相等。那么会发生什么?


// Tony


" Peter Duniho" < Np ********* @nnowslpianmk.comskrev i meddelandet

news:op *************** @ petes-computer.local ...
Hello!!

Now we assume that I store instances of Card as key in some sort of hash
table or similar things.

According to you will the hash table implementations treat two instances
that might have
compared as equal as if they are not equal.what will happen then?

//Tony

"Peter Duniho" <Np*********@nnowslpianmk.comskrev i meddelandet
news:op***************@petes-computer.local...

周五,2008年6月13日00:01:31 -0700,Tony< jo ************* ****@telia.com>

写道:
On Fri, 13 Jun 2008 00:01:31 -0700, Tony <jo*****************@telia.com>
wrote:

[...]

现在来我的问题我并不完全明白这个

GetHashCode用于什么目的。

这就是我再次提起它的原因。
在下面这个简单的例子中,如果我在这个Card类中使用了这个GetHashCode,那么如果我将b $ b跳过

将会发生什么?
[...]
Now to my question I don''t really fully understand what purpose this
GetHashCode is used for.
That''s why I bring it up again.
In this simple example below with the Card class what would happen if I
skip
this GetHashCode in this Card class?



然后哈希表实现会将两个可能与
进行比较的实例视为相等,就好像它们不相等一样。同样地,

可能会使用哈希码。


如果你从不使用这个类的实例作为哈希表中的键或者

其他任何关心哈希码的东西,都没关系。


如果你决定继续实施GetHashCode()(和恕我直言,为什么

不是吗?),你应该拼写正确的方法名称。 :)


如果您需要有关哈希码如何使用的更多信息,我会建议一个关于该主题的好的计算机科学文本。一个新闻组不是最好的方式来教育自己这样的事情,这是一个相对高级的话题

值得多于一个或几个新闻组帖子。


Pete


Then hash table implementations will treat two instances that might have
compared as equal as if they are not equal. Likewise anything else that
might use the hash code.

If you never use instances of this class as keys in a hash table or with
anything else that cares about the hash code, it won''t matter.

If you do decide to go ahead and implement GetHashCode() (and IMHO, why
not?), you should probably spell the name of the method correctly. :)

If you need more information about how hash codes are used, I''d suggest a
good computer science text on the subject. A newsgroup isn''t the best way
to educate oneself on such things, it being a relatively advanced topic
deserving of much more than one or a few newsgroup posts.

Pete



6月13日上午8:39,Tony < johansson.anders ... @ telia.comwrote:
On Jun 13, 8:39 am, "Tony" <johansson.anders...@telia.comwrote:

现在我们假设我将Card的实例存储为某种哈希的关键

表或类似的东西。


根据你的意思,哈希表实现会处理两个实例

可能有
比较等于他们不相等。那么会发生什么?
Now we assume that I store instances of Card as key in some sort of hash
table or similar things.

According to you will the hash table implementations treat two instances
that might have
compared as equal as if they are not equal.what will happen then?



假设您使用键10个黑桃存储了一些值。稍后你

创建一个新的但相等的10个黑桃 - 您希望能够使用新的10个黑桃来查看哈希表中的值,但是如果它

给出了不同的结果哈希代码到原始密钥,哈希表不会

找到条目。


Jon

Suppose you store some value with the key "10 of spades". Later you
create a new but equal "10 of spades" - you''d like to be able to look
up the value in the hash table using the new "10 of spades", but if it
gives a different hash code to the original key, the hashtable won''t
find the entry.

Jon


这篇关于如果我跳过实现GetHashCode会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆