Int32.GetHashCode 方法在 C# 中使用,带有示例


C# 中的 Int32.GetHashCode() 方法用于返回当前实例的哈希码。

语法

以下是语法 -

public override int GetHashCode ();

示例

现在让我们看一个示例来实现 Int32.GetHashCode() 方法 -

using System;
public class Demo {
   public static void Main(){
      int val1 = Int32.MinValue;
      int val2 = 300;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("Are they equal? = "+val1.Equals(val2));
      Console.WriteLine("HashCode for Value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for Value2 = "+val2.GetHashCode());
   }
}

输出

这将产生以下输出 -

Value1 = -2147483648
Value2 = 300
Are they equal? = False
HashCode for Value1 = -2147483648
HashCode for Value2 = 300

示例

现在让我们看另一个示例来实现 Int32.GetHashCode() 方法 -

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

输出

这将产生以下输出 -

Value1 = 50
Value2 = 50
Are they equal? = True
HashCode for Value1 = 50
HashCode for Value2 = 50

更新于: 11-11-2019

160 次浏览

开启您的 事业

通过完成课程获得认证

入门
广告