I'm not aware of such a thing being built-into .Net up to version 3.5, although .Net 4 is very likely to support it natively via the IStructuralEquatable interface which Array
will implement (thanks to Greg Beech for pointing that out).
Here's a simple implementation using an extension method on IEnumerable.
int HashContents<T>(this IEnumerable<T> enumerable)
{
int hash = 0x218A9B2C;
foreach (var item in enumerable)
{
int thisHash = item.GetHashCode();
//mix up the bits.
hash = thisHash ^ ((hash << 5) + hash);
}
return hash;
}
This will give different hashcodes for {0,0}
and {0,0,0}
.