Skip to content
Related Articles

Related Articles

C# | Byte.GetHashCode() Method
  • Last Updated : 11 Feb, 2019

This method is used to return the hash code for the given byte.

Syntax:

public override int GetHashCode ();

Return Value: This method returns a hash code for the current Byte.

Below programs illustrate the use of Byte.GetHashCode() Method:

Example 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);
        }
    }
}


Output:

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

Example 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("");
    }
}


Output:

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

Reference:

My Personal Notes arrow_drop_up
Recommended Articles
Page :