如何为布尔值实现GetHashCode?

Kry*_*hic 1 c# boolean gethashcode

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

Sco*_*ain 7

你可以看到.NET实际的源代码在这里,在implmentation为GetHashCode()一个布尔是

  private bool m_value;

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

  public override int GetHashCode() {
      return (m_value)?True:False;
  }
Run Code Online (Sandbox Code Playgroud)

(是的,这是不可思议的是System.Boolean包含了bool作为一个成员变量,在类被编译的CLR对待"原始"类型Boolean,Byte,SByte,Int16,UInt16,Int32,UInt32,Int64,UInt64,IntPtr,UIntPtr,Char,Double,和Single特殊所以他们可以做类似的东西)