C# | Método Byte.GetHashCode()

Este método se usa para devolver el código hash para el byte dado.
Sintaxis:

public override int GetHashCode ();

Valor de retorno: este método devuelve un código hash para el byte actual.
Los siguientes programas ilustran el uso del método Byte.GetHashCode() :
Ejemplo 1:

CSHARP

// C# program to demonstrate
// Byte.GetHashCode Method
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        // Declaring and initializing 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);
        }
    }
}
Producción: 

Hashcode: 223
Hashcode: 71
Hashcode: 1
Hashcode: 0
Hashcode: 0
Hashcode: 0
Hashcode: 0
Hashcode: 0

 

Ejemplo 2:

CSHARP

// 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("");
    }
}
Producción: 

long hashcode:- 95 33 0 0 0 0 0 0 
int Hashcode: 95 33 0 0 
byte hashcode:- 85 0

 

Referencia: 

Publicación traducida automáticamente

Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *