C# › Equality: Equals, GetHashCode & IEquatable

How does HashCode.Combine() help you write a correct and well-distributed GetHashCode() implementation?

Intermediate

Answer

HashCode.Combine() takes multiple field values and produces a well-mixed, evenly-distributed hash code using a proven algorithm — far better than naive approaches like simply XOR-ing fields together (which can produce poor distribution or even zero for identical field pairs), and is the modern recommended standard approach.
public override int GetHashCode() => HashCode.Combine(FirstName, LastName, BirthDate);
// vs. the old, worse approach:
// public override int GetHashCode() => FirstName.GetHashCode() ^ LastName.GetHashCode(); // poor distribution
Real-world example Writing a correct, well-distributed GetHashCode() override for a multi-field value object used as a Dictionary key.

Common follow-ups: What happens with HashCode.Combine() if you need to combine MORE than 8 values, given its overloads are limited?