什么是哈希code使用的呢?它是独一无二的? [英] what is hashCode use for? is it unique?

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

问题描述

我注意到有在每一个控制,物品,WP7,这回数序列的 getHash code()方法。我可以用这个哈希code,以确定一个项目?例如,我要识别图片或在设备中的歌曲,并行踪检查。如果具体项目给出的散列code是唯一可以这样做。

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.

你能不能帮我解释一下什么是哈希code和 getHash code()使用?

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

推荐答案

MSDN说

一个散列code是用于识别对象的数值
  在平等的测试。它也可以作为一个对象的索引
  在集合。

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.

的GetHash code方法适合于使用在哈希算法和
  数据结构,诸如哈希表

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

在GetHash code方法的默认实现不
  保证不同的对象唯一的返回值。此外,该
  .NET框架不保证的默认实现
  GetHash code方法,它返回值将介于相同
  不同版本的.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.

该GetHash code方法可以由派生类型重写。值
  类型必须重写此方法来提供一个哈希函数是
  适用于该类型,并提供一个有用的分布
  哈希表。对于唯一性,哈希code必须是基于价值
  实例字段或属性,而不是一个静态字段或
  属性。

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对象还必须覆盖对象
  GetHash code的方法,因为这些对象必须生成自己的哈希
  code。如果用作密钥的对象不提供有用
  实施GetHash code,你可以指定一个哈希code供应商
  当Hashtable对象构造。在此之前的.NET框架
  2.0版,哈希code提供商是基于
  System.Collections.IHash codeProvider接口。与版本开始
  2.0,散列code提供商是根据
  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.

基本上,哈希codeS存在,使哈希表可能。结果
两个相等的对象都保证有平等的哈希codeS。结果
两个不相等的对象的不可以保证有不平等的哈希codeS(这就是所谓的冲突)。

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).

这篇关于什么是哈希code使用的呢?它是独一无二的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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