Gorgon
Show / Hide Table of Contents

Class GorgonHashGenerationExtension

Extension method to generate hash codes for integer values.

Inheritance
object
GorgonHashGenerationExtension
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: Gorgon.Core
Assembly: Gorgon.Core.dll
Syntax
public static class GorgonHashGenerationExtension

Methods

| Edit this page View Source

GenerateHash<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);
}
  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author