简体   繁体   中英

How is GetHashCode Implemented for Booleans?

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

You can see the actual source code for .NET here , the implmentation for GetHashCode() for a bool is

  private bool m_value;

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

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

(And yes, it is weird that System.Boolean contains a bool as a member variable, when the class is compiled the CLR treats the "primitive" types Boolean , Byte , SByte , Int16 , UInt16 , Int32 , UInt32 , Int64 , UInt64 , IntPtr , UIntPtr , Char , Double , and Single special so they can do stuff like that)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM