在.NET GetHashCode方法中,很多地方都使用.NET 方法.特别是在快速查找集合中的项目或确定相等性时.是否有关于如何GetHashCode为我的自定义类实现覆盖的标准算法/最佳实践,因此我不会降低性能?
在以下场景中,与下面的函数发生HashCode冲突的可能性有多大.
假设我们有1000万个对象.
int[] key=new int[4];
public override int GetHashCode()
{
// Use large prime multiples to create a unique hash key
// Create the hash offsets using a "even powers of 2 minus 1" method, which gives
// primes most of the time.
int hashKey = 0;
hashKey += 2047 * key[0];
hashKey += 8191 * key[1];
hashKey += 32767 * key[2];
hashKey += 131071 * key[3];
return hashKey; …Run Code Online (Sandbox Code Playgroud)