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

📅  最后修改于: 2021-05-29 23:34:49             🧑  作者: Mango

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

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

范例1:

// C# program to illustrate the
// Int16.GetHashCode() Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Taking Int16 variable
        // i.e. short data type
        short s1 = 124;
  
        // Getting the hash code for Int16
        // using GetHashCode() method
        int result = s1.GetHashCode();
  
        // Display the HashCode
        Console.WriteLine("HashCode for Int16 is: {0}",
                                               result);
    }
}
输出:
HashCode for Int16 is: 8126588

范例2:

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

参考:

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