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


C# CharEnumerator.GetHashCode()用法及代碼示例


GetHashCode()方法用作默認的哈希函數,並返回當前對象的哈希碼。此方法從Object類繼承。

用法:

public virtual int GetHashCode ();

返回值:此方法返回一個與當前對象的哈希碼相對應的Int32值。


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

示例1:

// C# program to illustrate the use 
// of CharEnumerator.GetHashCode() 
// Method 
using System; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
        // Initialize a string object 
        string str = "GeeksforGeeks is fun"; 
  
        // Instantiate a CharEnumerator object 
        CharEnumerator chEnum1 = str.GetEnumerator(); 
  
        // Instantiate another CharEnumerator object 
        CharEnumerator chEnum2 = str.GetEnumerator(); 
  
        // Printing the Hash Code of 
        // both the CharEnumerator objects 
        Console.WriteLine(chEnum1.GetHashCode()); 
        Console.WriteLine(chEnum2.GetHashCode()); 
    } 
}
輸出:
-381312627
1646495825

示例2:

// C# program to illustrate the use 
// of CharEnumerator.GetHashCode() 
// Method 
using System; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
        // Initialize two string object 
        string str1 = "GeeksforGeeks is fun", 
                  str2 = "C C++ Java Python"; 
  
        // Instantiate a CharEnumerator object 
        CharEnumerator chEnum1 = str1.GetEnumerator(); 
  
        // Instantiate another CharEnumerator object 
        CharEnumerator chEnum2 = str2.GetEnumerator(); 
  
        // Printing the Hash Code of 
        // both the CharEnumerator objects 
        Console.WriteLine(chEnum1.GetHashCode()); 
        Console.WriteLine(chEnum2.GetHashCode()); 
    } 
}
輸出:
491910500
-1775248344


相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 C# | CharEnumerator.GetHashCode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。