📜  C#|具有示例的Char.GetHashCode()方法

📅  最后修改于: 2021-05-29 21:00:59             🧑  作者: Mango

此方法用于返回此实例的哈希码。

句法:

public override int GetHashCode ();

返回值:该方法返回32位有符号整数哈希码。

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

范例1:

// C# program to demonstrate
// Char.GetHashCode() Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // declaring and initializing char
        char ch1 = 'B';
  
        // checking condition
        // using Equals() Method
        int val = ch1.GetHashCode();
  
        // Display Hashcode
        Console.WriteLine("Hashcode :- {0}", val);
    }
}
输出:
Hashcode :- 4325442

范例2:

// C# program to demonstrate
// Char.GetHashCode() Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling hash() Method
        hash('a');
        hash('b');
        hash('c');
        hash('x');
        hash('y');
        hash('z');
    }
  
    // Defining hash() Method
    public static void hash(char ch)
    {
        // checking condition
        // using Equals() Method
        int val = ch.GetHashCode();
  
        // Display Hashcode
        Console.WriteLine("Hashcode of " + ch +
                                " :- {0}", val);
    }
}
输出:
Hashcode of a :- 6357089
Hashcode of b :- 6422626
Hashcode of c :- 6488163
Hashcode of x :- 7864440
Hashcode of y :- 7929977
Hashcode of z :- 7995514

参考:

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