Int16.GetHashCode() 方法在 C# 中带示例


Int16.GetHashCode() 方法用于返回当前实例的哈希码。

语法

以下是 Int16.GetHashCode() 方法的语法 −

public override int GetHashCode ();

示例

现在让我们看一个示例,以实现 Int16.GetHashCode() 方法 −

using System;
public class Demo {
   public static void Main(){
      short val1 = 20;
      short val2 = 25;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
   }
}

输出

这将产生以下输出 −

Value1 = 20
Value2 = 25
HashCode for value1 = 1310740
HashCode for value2 = 1638425
Are they equal? = False

示例

现在让我们再看一个示例,以实现 Int16.GetHashCode() 方法 −

using System;
public class Demo {
   public static void Main(){
      short val1 = 0;
      short val2 = Int16.MaxValue;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
   }
}

输出

这将产生以下输出 −

Value1 = 0
Value2 = 32767
HashCode for value1 = 0
HashCode for value2 = 2147450879
Are they equal? = False

更新日期:2019 年 11 月 11 日

67 查看

开启你的 职业

获得证书,完成此课程

开始
广告