Skip to content

Instantly share code, notes, and snippets.

@kpol
Created September 19, 2012 23:06
Show Gist options
  • Select an option

  • Save kpol/3752921 to your computer and use it in GitHub Desktop.

Select an option

Save kpol/3752921 to your computer and use it in GitHub Desktop.
CombineHashCodes
public static int CombineHashCodes(params int[] hashCodes)
{
if (hashCodes == null)
{
throw new ArgumentNullException("hashCodes");
}
if (hashCodes.Length == 0)
{
throw new IndexOutOfRangeException();
}
if (hashCodes.Length == 1)
{
return hashCodes[0];
}
var result = hashCodes[0];
for (var i = 1; i < hashCodes.Length; i++)
{
result = CombineHashCodes(result, hashCodes[i]);
}
return result;
}
private static int CombineHashCodes(int h1, int h2)
{
return (h1 << 5) + h1 ^ h2;
//unchecked
//{
// var hash = 17;
// hash = hash * 23 + h1;
// hash = hash * 23 + h2;
// return hash;
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment