📜  C#中的Int32.GetHashCode方法与示例

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

Int32.GetHashCode方法用于获取当前Int32实例的HashCode。

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# program to illustrate the
// Int32.GetHashCode() Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Taking Int32 variable
        int val = 32523;
  
        // Getting the hash code for Int32
        // using GetHashCode() method
        int result = val.GetHashCode();
  
        // Display the hashcode
        Console.WriteLine("HashCode for Int32 is: {0}",
                                               result);
    }
}
输出:
HashCode for Int32 is: 32523

范例2:

// C# program to illustrate the
// Int32.GetHashCode() Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // using result() Method
        result(Int32.MinValue);
        result(Int32.MaxValue);
    }
  
    // result() method
    public static void result(int val)
    {
  
        // using GetHashCode() method
        int code = val.GetHashCode();
  
        // Display the hashcode
        Console.WriteLine("HashCode for {0} is {1}",
                                         val, code);
    }
}
输出:
HashCode for -2147483648 is -2147483648
HashCode for 2147483647 is 2147483647

参考:

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