(x, y).GetHashCode() 如何在幕后工作?

How does (x, y).GetHashCode() work behind the scenes?

public class Lemon{
   public int Ounces;
   public string Color;

   public override int GetHashCode() => (Ounces, Color).GetHashCode();
}

我很好奇它是如何工作的。 (Ounces, Color) 类似于匿名类型,但语法不同。如果它是匿名类型,那么我仍然不确定它是如何知道获得唯一哈希值的。

A link 相关的 .net 源代码会很棒。很难发现,因为我不确定 (Ounces, Color) 最终被编译成什么类型​​。

(Ounces, Color)是元组,在C#7中引入,对应的类型是ValueTuple<T1, T2>。从 reference source,你可以看出 GetHashCode() 正在通过使用

组合每个对象(和一个额外的随机种子)的哈希码来计算哈希码
 public static int Combine(int h1, int h2)
 {
     uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27);
     return ((int)rol5 + h1) ^ h2;
 }