此方法用於返回給定字節的哈希碼。
用法:
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
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Byte.GetHashCode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。