【问题标题】:How is GetHashCode Implemented for Booleans?布尔值如何实现 GetHashCode?
【发布时间】:2016-07-22 08:10:06
【问题描述】:

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

【问题讨论】:

标签: c# boolean gethashcode


【解决方案1】:

您可以看到.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 作为成员变量,当编译该类时,CLR 处理“原始”类型BooleanByteSByte、@ 987654329@、UInt16Int32UInt32Int64UInt64IntPtrUIntPtrUIntPtrCharSingleSingle,他们可以做一些特别的事情,比如4339@那个)

【讨论】:

    猜你喜欢
    • 2011-10-13
    • 2017-02-08
    • 2011-09-06
    • 2013-10-25
    • 2011-04-23
    • 2023-03-24
    • 1970-01-01
    • 2011-02-22
    • 2013-07-06
    相关资源
    最近更新 更多