Created
January 14, 2021 16:54
-
-
Save pwelter34/e8b44e95714dfa5d52286249d525bfa7 to your computer and use it in GitHub Desktop.
Generate hash code
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 readonly struct HashCode | |
{ | |
private readonly int _value; | |
private HashCode(int value) => _value = value; | |
public static HashCode Seed { get; } = new HashCode(17); | |
public HashCode Combine<T>(T obj) | |
{ | |
var h = EqualityComparer<T>.Default.GetHashCode(obj); | |
return unchecked(new HashCode((_value * 31) + h)); | |
} | |
public override int GetHashCode() => _value; | |
public static implicit operator int(HashCode hash) => hash._value; | |
} |
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 class Person | |
{ | |
public string FirstName { get; set;} | |
public string LastName { get; set; } | |
public override int GetHashCode() | |
{ | |
return HashCode.Seed | |
.Combine(FirstName) | |
.Combine(LastName) | |
.GetHashCode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment