Skip to content

Instantly share code, notes, and snippets.

@riyadparvez
Created January 9, 2013 10:48
Show Gist options
  • Save riyadparvez/4492272 to your computer and use it in GitHub Desktop.
Save riyadparvez/4492272 to your computer and use it in GitHub Desktop.
Best algorithm to override GetHashCode()
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + field1.GetHashCode();
hash = hash * 23 + field2.GetHashCode();
hash = hash * 23 + field3.GetHashCode();
return hash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment