Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetHashCode() on byte[] array

Tags:

c#

hash

What does GetHashCode() calculate when invoked on the byte[] array? The 2 data arrays with equal content do not provide the same hash.

like image 630
Chesnokov Yuriy Avatar asked Aug 30 '11 14:08

Chesnokov Yuriy


People also ask

How to hash byte Array java?

hashCode(byte[]) method returns a hash code based on the contents of the specified array. For any two byte arrays a and b such that Arrays. equals(a, b), it is also the case that Arrays. hashCode(a) == Arrays.

What is the purpose of GetHashCode?

The GetHashCode method provides this hash code for algorithms that need quick checks of object equality. For information about how hash codes are used in hash tables and for some additional hash code algorithms, see the Hash Function entry in Wikipedia. Two objects that are equal return hash codes that are equal.


2 Answers

Arrays in .NET don't override Equals or GetHashCode, so the value you'll get is basically based on reference equality (i.e. the default implementation in Object) - for value equality you'll need to roll your own code (or find some from a third party). You may want to implement IEqualityComparer<byte[]> if you're trying to use byte arrays as keys in a dictionary etc.

EDIT: Here's a reusable array equality comparer which should be fine so long as the array element handles equality appropriately. Note that you mustn't mutate the array after using it as a key in a dictionary, otherwise you won't be able to find it again - even with the same reference.

using System; using System.Collections.Generic;  public sealed class ArrayEqualityComparer<T> : IEqualityComparer<T[]> {     // You could make this a per-instance field with a constructor parameter     private static readonly EqualityComparer<T> elementComparer         = EqualityComparer<T>.Default;      public bool Equals(T[] first, T[] second)     {         if (first == second)         {             return true;         }         if (first == null || second == null)         {             return false;         }         if (first.Length != second.Length)         {             return false;         }         for (int i = 0; i < first.Length; i++)         {             if (!elementComparer.Equals(first[i], second[i]))             {                 return false;             }         }         return true;     }      public int GetHashCode(T[] array)     {         unchecked         {             if (array == null)             {                 return 0;             }             int hash = 17;             foreach (T element in array)             {                 hash = hash * 31 + elementComparer.GetHashCode(element);             }             return hash;         }     } }  class Test {     static void Main()     {         byte[] x = { 1, 2, 3 };         byte[] y = { 1, 2, 3 };         byte[] z = { 4, 5, 6 };          var comparer = new ArrayEqualityComparer<byte>();          Console.WriteLine(comparer.GetHashCode(x));         Console.WriteLine(comparer.GetHashCode(y));         Console.WriteLine(comparer.GetHashCode(z));         Console.WriteLine(comparer.Equals(x, y));         Console.WriteLine(comparer.Equals(x, z));     } } 
like image 56
Jon Skeet Avatar answered Oct 12 '22 23:10

Jon Skeet


Like other non-primitive built-in types, it just returns something arbitrary. It definitely doesn't try to hash the contents of the array. See this answer.

like image 44
mqp Avatar answered Oct 12 '22 22:10

mqp