Skip to content

Instantly share code, notes, and snippets.

@webtk
Created July 28, 2018 06:06
Show Gist options
  • Save webtk/a6bca8d9eb3842581324925375761ed2 to your computer and use it in GitHub Desktop.
Save webtk/a6bca8d9eb3842581324925375761ed2 to your computer and use it in GitHub Desktop.
C# Get Hashcode Simply Short
// 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