Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Object.GetHashCode Method

Definition

Namespace:
System
Assemblies:
mscorlib.dll, System.Runtime.dll
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Runtime.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
Source:
Object.cs
Source:
Object.cs
Source:
Object.cs
Source:
Object.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Serves as the default hash function.

public: virtual int GetHashCode();
public virtual int GetHashCode();
abstract member GetHashCode : unit -> intoverride this.GetHashCode : unit -> int
Public Overridable Function GetHashCode () As Integer

Returns

A hash code for the current object.

Remarks

For more information about this API, seeSupplemental API remarks for Object.GetHashCode.

Notes to Inheritors

A hash function is used to quickly generate a number (hash code) that corresponds to the value of an object. Hash functions are usually specific to each type and, for uniqueness, must use at least one of the instance fields as input. Hash codes should not be computed by using the values of static fields.

For classes derived fromObject, theGetHashCode method can delegate to the base classGetHashCode() implementation only if the derived class defines equality to be reference equality. The default implementation ofGetHashCode() for reference types returns a hash code that is equivalent to the one returned by theGetHashCode(Object) method. You can overrideGetHashCode() for immutable reference types. In general, for mutable reference types, you should overrideGetHashCode() only if:

  • You can compute the hash code from fields that are not mutable; or

  • You can ensure that the hash code of a mutable object does not change while the object is contained in a collection that relies on its hash code.

Otherwise, you might think that the mutable object is lost in the hash table. If you do choose to overrideGetHashCode() for a mutable reference type, your documentation should make it clear that users of your type should not modify object values while the object is stored in a hash table.

For value types,GetHashCode() provides a default hash code implementation that uses reflection. You should consider overriding it for better performance.

For more information and examples that compute hash codes in a variety of ways, see the Examples section.

A hash function must have the following properties:

  • If two objects compare as equal, theGetHashCode() method for each object must return the same value. However, if two objects do not compare as equal, theGetHashCode() methods for the two objects do not have to return different values.

  • TheGetHashCode() method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object'sSystem.Object.Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.

  • For the best performance, a hash function should generate an even distribution for all input, including input that is heavily clustered. An implication is that small modifications to object state should result in large modifications to the resulting hash code for best hash table performance.

  • Hash functions should be inexpensive to compute.

  • TheGetHashCode() method should not throw exceptions.

For example, the implementation of theGetHashCode() method provided by theString class returns identical hash codes for identical string values. Therefore, twoString objects return the same hash code if they represent the same string value. Also, the method uses all the characters in the string to generate reasonably randomly distributed output, even when the input is clustered in certain ranges (for example, many users might have strings that contain only the lower 128 ASCII characters, even though a string can contain any of the 65,535 Unicode characters).

Providing a good hash function on a class can significantly affect the performance of adding those objects to a hash table. In a hash table with keys that provide a good implementation of a hash function, searching for an element takes constant time (for example, an O(1) operation). In a hash table with a poor implementation of a hash function, the performance of a search depends on the number of items in the hash table (for example, an O(n) operation, wheren is the number of items in the hash table). A malicious user can input data that increases the number of collisions, which can significantly degrade the performance of applications that depend on hash tables, under the following conditions:

  • When hash functions produce frequent collisions.

  • When a large proportion of objects in a hash table produce hash codes that are equal or approximately equal to one another.

  • When users input the data from which the hash code is computed.

Derived classes that overrideGetHashCode() must also overrideEquals(Object) to guarantee that two objects considered equal have the same hash code; otherwise, theHashtable type might not work correctly.

Applies to

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo