Created
July 28, 2018 06:06
-
-
Save webtk/a6bca8d9eb3842581324925375761ed2 to your computer and use it in GitHub Desktop.
C# Get Hashcode Simply Short
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
// https://stackoverflow.com/questions/23468671/what-is-the-best-way-to-implement-gethashcode-for-class-with-lots-of-propertie | |
public static class HashCodeByPropertyExtensions | |
{ | |
public static int GetHashCodeOnProperties<T>(this T inspect) | |
{ | |
return inspect.GetType().GetProperties().Select(o => o.GetValue(inspect, null)).GetListHashCode(); | |
} | |
public static int GetListHashCode<T>(this IEnumerable<T> sequence) | |
{ | |
return sequence | |
.Select(item => item.GetHashCode()) | |
.Aggregate((total, nextCode) => total ^ nextCode); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment