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:
// 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
filter_none
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(""); } } |
chevron_right
filter_none
Output:
long hashcode:- 95 33 0 0 0 0 0 0 int Hashcode: 95 33 0 0 byte hashcode:- 85 0
Reference:
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | CopyTo() Method
- C# | Random.Next() Method
- C# | Trim() Method
- C# | Uri.IsHexDigit() Method
- C# | Uri.FromHex() Method
- C# | Join() Method | Set - 1
- C# | PadLeft() Method
- C# | Uri.IsBaseOf(Uri) Method
- C# | IndexOfAny() Method
- C# | SortedDictionary.Add() Method
- C# | Math.Pow() Method
- C# | Copy() Method
- C# | Join Method | Set - 2
- C# | Method Overriding
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.



