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


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


此方法用于返回给定字节的哈希码。

用法:

public override int GetHashCode ();

返回值:此方法返回当前字节的哈希码。


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

示例1:

// C# program to demonstrate 
// Byte.GetHashCode Method  
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Declaring and initalizing val 
        long val = 83935; 
  
        // converting long array into bytes 
        byte[] bytes = BitConverter.GetBytes(val); 
  
        // getting hashcode 
        // using GetHashCode() method 
        for (int i = 0; i < bytes.Length; i++) { 
  
            // getting hash code of a single byte 
            int j = bytes[i].GetHashCode(); 
  
            Console.WriteLine("Hashcode: {0}", j); 
        } 
    } 
}
输出:
Hashcode: 223
Hashcode: 71
Hashcode: 1
Hashcode: 0
Hashcode: 0
Hashcode: 0
Hashcode: 0
Hashcode: 0

示例2:

// C# program to demonstrate 
// Byte.GetHashCode Method  
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        check((long)8543, "long"); 
        check((int)8543, "int"); 
        check((byte)85, "byte"); 
    } 
  
    // Defining the check method 
    public static void check(long val, string name) 
    { 
  
        // converting long array into bytes 
        byte[] bytes = BitConverter.GetBytes(val); 
  
        // getting hashcode 
        // using GetHashCode() method 
        Console.Write(name + " hashcode:- "); 
        for (int i = 0; i < bytes.Length; i++) { 
  
            // getting hash code of a single byte 
            int j = bytes[i].GetHashCode(); 
            Console.Write("{0} ", j); 
        } 
        Console.WriteLine(""); 
    } 
  
    // Defining the check method 
    public static void check(int val, string name) 
    { 
  
        // converting long array into bytes 
        byte[] bytes = BitConverter.GetBytes(val); 
  
        // getting hashcode 
        // using GetHashCode() method 
        Console.Write(name + " Hashcode: "); 
        for (int i = 0; i < bytes.Length; i++) { 
  
            // getting hash code of a single byte 
            int j = bytes[i].GetHashCode(); 
            Console.Write("{0} ", j); 
        } 
        Console.WriteLine(""); 
    } 
  
    // Defining the check method 
    public static void check(byte val, string name) 
    { 
  
        // converting long array into bytes 
        byte[] bytes = BitConverter.GetBytes(val); 
  
        // getting hashcode 
        // using GetHashCode() method 
        Console.Write(name + " hashcode:- "); 
        for (int i = 0; i < bytes.Length; i++) { 
  
            // getting hash code of a single byte 
            int j = bytes[i].GetHashCode(); 
            Console.Write("{0} ", j); 
        } 
        Console.WriteLine(""); 
    } 
}
输出:
long hashcode:- 95 33 0 0 0 0 0 0 
int Hashcode: 95 33 0 0 
byte hashcode:- 85 0

参考:



相关用法


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