Class GorgonHashGenerationExtension
Extension method to generate hash codes for integer values.
Inherited Members
Namespace: Gorgon.Core
Assembly: Gorgon.Core.dll
Syntax
public static class GorgonHashGenerationExtension
Methods
| Edit this page View SourceGenerateHash<T>(int, T)
Function to build upon the hash code from a value.
Declaration
[Obsolete("Use System.HashCode.Combine instead.")]
public static int GenerateHash<T>(this int previousHash, T item)
Parameters
Type | Name | Description |
---|---|---|
int | previousHash | The hash code of the previous value. |
T | item | New item to add to the hash code. |
Returns
Type | Description |
---|---|
int | The hash code for the value. |
Type Parameters
Name | Description |
---|---|
T | Type of value to generate a hash code from. |
Remarks
The common hash code generation equation is usually: value1 ^ value2 ^ value3
. This leads to issues where hash code values don't have a uniform representation. This method
is meant to help provide a more even distribution of values.
To use this method when generating your hash code pick a prime number and call the method with it:
public int override GetHashCode()
{
// Here we've picked a prime number and are using that as a basis for our hash code, then we chain together calls
// in a fluent interface to combine the hash codes of the other members in this class.
return 281.GenerateHash(aMemberOfYourClass).GenerateHash(anotherMemberOfYourClass);
}