Created
January 9, 2013 10:48
-
-
Save riyadparvez/4492272 to your computer and use it in GitHub Desktop.
Best algorithm to override GetHashCode()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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