C# | Byte.GetHashCode() Method

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.