當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。