Skip to content

Instantly share code, notes, and snippets.

@pwelter34
Created January 14, 2021 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwelter34/e8b44e95714dfa5d52286249d525bfa7 to your computer and use it in GitHub Desktop.
Save pwelter34/e8b44e95714dfa5d52286249d525bfa7 to your computer and use it in GitHub Desktop.
Generate hash code
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;
}
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