当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Int16.GetHashCode()用法及代码示例


C# 中的 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

相关用法


注:本文由纯净天空筛选整理自AmitDiwan大神的英文原创作品 Int16.GetHashCode() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。