What does GetHashCode()
calculate when invoked on the byte[]
array?
The 2 data arrays with equal content do not provide the same hash.
5 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));
}
}
-
-
thank you very much for the useful snippet. A bit off the topic if you please, your C# in depth book is very intresting, I'm going to read it. The g+ idea is superb introducing circles, compared to facebook, where you can not separate your contacts. It would be great to show different user page account content and information to every circle, e.g. one would not be happy to show some of work circle page content to his friends and vice versa. Can you advise if soon will we be able to register there? Aug 31, 2011 at 4:40
-
1@Chesnokov: That's a bit off-topic for here, I'm afraid - and I wouldn't be able to tell you about any upcoming features anyway. Aug 31, 2011 at 5:22
-
4There seems to be some debate on whether
GetHashCode
should scan over the entire sequence. Interestingly, the internal implementation forArray.IStructuralEquatable.GetHashCode
only considers the last eight items of an array, sacrificing hash uniqueness for speed.– DouglasFeb 13, 2016 at 17:26 -
1I did something similar using Enumerable.SequenceEqual(). Is there a particular reason to hand-code the element comparison? (Admittedly it's probably a bit faster.) Feb 15, 2017 at 19:15
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.
byte[]
inherits GetHashCode()
from object
, it doesn't override it. So what you get is basically object
's implementation.
Simple solution
public static int GetHashFromBytes(byte[] bytes)
{
return new BigInteger(bytes).GetHashCode();
}
-
4Seeing this solution made me smile. Clean, elegant. Digging deeper the hash implementation ends up calling github.com/microsoft/referencesource/blob/master/… Apr 17, 2020 at 8:22
-
1
-
1@DaveJellison There is a
(2^32)
in 1 chance of collision, which is negalegible for most scenarios but is something that must be kept in mind whenever there's a hash code.– fjch1997Dec 26, 2020 at 3:14 -
1Agreed, but this is inherent with hashing as a rule. It's like going to the dictionary.com to complain about the definition of a word. Jan 2, 2021 at 13:49
-
1Note this method incurs a copy of the whole byte array, so may not be efficient. Also It's important to understand the purpose of GetHashCode() - it's not intended to produce a unique value but rather a well-distributed value for allocating buckets in a Dictionary or HashSet, which benefit from each bucket being roughly equal size. Both types use a combination of GetHashCode() and Equals() to determine whether a collision has really occurred. Sep 3, 2021 at 11:21
If it's not the same instance, it will return different hashes. I'm guessing it is based on the memory address where it is stored somehow.
-
no, it is not the same instance, I presume in that case hashes would be equal Aug 30, 2011 at 20:06
IStructuralEquatable
requires use of the non-genericIEqualityComparer
interface, which means that eachbyte
in the array is going to be boxed during computation of the hashcode.