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


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


Type.GetHashCode()方法用于返回此实例的哈希码。

用法: public override int GetHashCode ();

返回值:此方法返回当前实例的哈希码。


以下示例程序旨在说明Type.GetHashCode()方法的使用:

示例1:

// C# program to demonstrate the 
// Type.GetHashCode() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
using System.Collections.Generic; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(Dictionary<, >); 
  
        // Getting hashcode of given type 
        // using GetHashCode() Method 
        int hash = objType.GetHashCode(); 
  
        // Display the Result 
        Console.Write("Hashcode is {0}", hash); 
    } 
}
输出:
Hashcode is 32340152

示例2:

// C# program to demonstrate the 
// Type.GetHashCode() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // calling get() method 
        get(typeof(int)); 
        get(typeof(string)); 
        get(typeof(decimal)); 
        get(typeof(float)); 
    } 
  
    // defining get() method 
    public static void get(Type objType) 
    { 
  
        // Getting hashcode of given type 
        // using GetHashCode() Method 
        int hash = objType.GetHashCode(); 
  
        // Display the Result 
        Console.WriteLine("Hashcode of {0} is {1}", objType, hash); 
    } 
}
输出:
Hashcode of System.Int32 is 18784216
Hashcode of System.String is 18793840
Hashcode of System.Decimal is 19137544
Hashcode of System.Single is 18792864

参考:



相关用法


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