string.GetHashCode() 会返回负值吗?
我尝试使用一批随机字符串,得到的所有值都是正数,但我想知道:
String.GetHashCode()
会返回负数还是0?
由于返回值是int,所以我猜测可能是这样,所以如果是这样的话,我必须改变我的逻辑。
如果您有答案或有一些官方来源,请分享
I tried with batch of random strings, all values I got are positive, but I wondering:
Will String.GetHashCode()
return negative or 0?
Since the return value is int, so I guess it might be, so if it is the case, I have to change my logic.
If you have answer or have some official sources, please share
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它可以返回负值。
您不得拥有任何适用于
GetHashCode()
值的逻辑。GetHashCode()
不保证唯一,并且可能在构建之间发生变化。GetHashCode()
必须被视为不透明令牌,可以与其他哈希组合或修改为哈希表。Yes, it can return negative values.
You must not have any logic that works with
GetHashCode()
values.GetHashCode()
is not guaranteed to be unique and can change between builds.GetHashCode()
must be treated as an opaque token that can be combined with other hashes or modded out into hashtables.它可以返回负值(基于msdn):
http://msdn.microsoft.com/en-us/library/system.string。 gethashcode.aspx
It can return negative value (based on msdn):
http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx