📜  C#中的Boolean.GetHashCode()方法(带示例)

📅  最后修改于: 2021-05-30 00:35:19             🧑  作者: Mango

Boolean.GetHashCode()方法用于返回此实例的哈希码。

下面的程序说明了Boolean.GetHashCode()方法的用法:

范例1:

// C# program to demonstrate the
// Boolean.GetHashCode() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring and initializing value1
        bool value1 = true;
  
        // getting the HashCode
        // using GetHashCode() method
        int value = value1.GetHashCode();
  
        // Display the HashCode
        Console.WriteLine("HashCode is {0}", value);
    }
}
输出:
HashCode is 1

范例2:

// C# program to demonstrate the
// Boolean.GetHashCode() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(true);
        get(false);
    }
  
    // defining get() method
    public static void get(bool value1)
    {
  
        // getting the HashCode
        // using GetHashCode() method
        int value = value1.GetHashCode();
  
        // Display the HashCode
        Console.WriteLine("HashCode is {0}", value);
    }
}
输出:
HashCode is 1
HashCode is 0

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.boolean.gethashcode?view=netframework-4.8