布尔值的 GetHashCode 是如何实现的?

How is GetHashCode Implemented for Booleans?

我想知道他们究竟是如何从 C#/.NET 中的布尔类型生成哈希码的?

您可以查看 .NET here 的实际源代码,GetHashCode() 的 bool 实现是

  private bool m_value;

  internal const int True = 1; 
  internal const int False = 0; 

  public override int GetHashCode() {
      return (m_value)?True:False;
  }

(是的,奇怪的是 System.Boolean 包含一个 bool 作为成员变量,当编译 class 时,CLR 将 "primitive" 类型 Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64IntPtrUIntPtrCharDoubleSingle 很特别,所以他们可以做那样的事情)