Last active
November 12, 2022 21:26
-
-
Save Andrey2G/bb272e27165b552e2a8a17f52050fb6f to your computer and use it in GitHub Desktop.
Overriding GetHashCode
This file contains 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 class MyValueObject:ValueObject<MyValueObject> | |
{ | |
public int field1{get;} | |
public double field2{get;} | |
public string field3{get;} | |
public override int GetHashCode() | |
{ | |
//don't forget to check fields for null | |
unchecked | |
{ | |
int hash = (int) 2166136261; | |
hash = (hash * 16777619) ^ field1.GetHashCode(); | |
hash = (hash * 16777619) ^ field2.GetHashCode(); | |
hash = (hash * 16777619) ^ field3.GetHashCode(); | |
return hash; | |
} | |
} | |
//*************** | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and for arrays!
https://stackoverflow.com/questions/638761/gethashcode-override-of-object-containing-generic-array/639098#639098