hashCode的作用是什么?它独特吗? [英] What is hashCode used for? Is it unique?

查看:76
本文介绍了hashCode的作用是什么?它独特吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到WP7中的每个控件,项中都有一个 getHashCode()方法,该方法返回一个数字序列。我可以使用该哈希码来识别商品吗?例如,我想识别设备中的图片或歌曲,并检查其位置。如果为特定项目指定的哈希码是唯一的,则可以这样做。

I notice there is a getHashCode() method in every controls, items, in WP7, which return a sequence of number. Can I use this hashcode to identify an item? For example I want to identify a picture or a song in the device, and check it whereabout. This could be done if the hashcode given for specific items is unique.

您能帮我解释一下什么是hashCode和 getHashCode()用于?

Can you help explain to me what is hashCode and getHashCode() use for?

推荐答案

MSDN说


哈希码是一个数值,用于在相等性测试期间标识对象
。它也可以用作集合中对象
的索引。

A hash code is a numeric value that is used to identify an object during equality testing. It can also serve as an index for an object in a collection.

GetHashCode方法适用于哈希算法和
数据结构

The GetHashCode method is suitable for use in hashing algorithms and data structures such as a hash table.

GetHashCode方法的默认实现不保证
可以保证不同对象的唯一返回值。此外,
.NET Framework不保证
GetHashCode方法的默认实现,并且
不同版本的.NET Framework之间返回的值相同。因此,不得将此方法的默认
实现用作哈希目的的唯一对象
标识符。

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

可以覆盖GetHashCode方法通过派生类型。值
类型必须重写此方法,以提供适用于该类型的
哈希函数,并在
哈希表中提供有用的分布。为了唯一,哈希码必须基于实例字段或属性的值
而不是静态字段或
属性。

The GetHashCode method can be overridden by a derived type. Value types must override this method to provide a hash function that is appropriate for that type and to provide a useful distribution in a hash table. For uniqueness, the hash code must be based on the value of an instance field or property instead of a static field or property.

对象在Hashtable对象中用作键的对象还必须重写
GetHashCode方法,因为这些对象必须生成自己的哈希
代码。如果用作键的对象不能提供GetHashCode的有用
实现,则可以在构造Hashtable对象时指定哈希码提供程序
。在.NET Framework
2.0版之前,哈希码提供程序基于
System.Collections.IHashCodeProvider接口。从版本
2.0开始,哈希码提供程序基于
System.Collections.IEqualityComparer接口。

Objects used as a key in a Hashtable object must also override the GetHashCode method because those objects must generate their own hash code. If an object used as a key does not provide a useful implementation of GetHashCode, you can specify a hash code provider when the Hashtable object is constructed. Prior to the .NET Framework version 2.0, the hash code provider was based on the System.Collections.IHashCodeProvider interface. Starting with version 2.0, the hash code provider is based on the System.Collections.IEqualityComparer interface.

基本上,存在哈希码以使哈希表成为可能。

保证两个相等的对象具有相等的哈希码。

保证两个不相等的对象保证具有不相等的哈希码(称为冲突)。

Basically, hash codes exist to make hashtables possible.
Two equal objects are guaranteed to have equal hashcodes.
Two unequal objects are not guaranteed to have unequal hashcodes (that's called a collision).

这篇关于hashCode的作用是什么?它独特吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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