HashCode.Combine 是可交换的吗?
Is HashCode.Combine commutative?
具体是
HashCode.Combine<T1,T2>(T1, T2)
交换? IE。是真的吗
HashCode.Combine(a, b) == HashCode.Combine(b, a)
for any a
and b
?
为什么不进行一个简单的实验,例如
public static void Experiment() {
for (int i = 0; i < 100; ++i)
for (int j = 0; j < 100; ++j) {
int a = HashCode.Combine(i, j);
int b = HashCode.Combine(j, i);
if (a != b) {
Console.Write($"Combine({i}, {j}) = {a} != {b} = Combine({j}, {i})");
return;
}
}
Console.Write("Seems to be commutative");
}
结果:
Combine(0, 1) = -839320321 != 864340100 = Combine(1, 0)
所以Hash.Combine
(当前实现,.Net Core 3.1)不是可交换的
具体是
HashCode.Combine<T1,T2>(T1, T2)
交换? IE。是真的吗
HashCode.Combine(a, b) == HashCode.Combine(b, a)
for any a
and b
?
为什么不进行一个简单的实验,例如
public static void Experiment() {
for (int i = 0; i < 100; ++i)
for (int j = 0; j < 100; ++j) {
int a = HashCode.Combine(i, j);
int b = HashCode.Combine(j, i);
if (a != b) {
Console.Write($"Combine({i}, {j}) = {a} != {b} = Combine({j}, {i})");
return;
}
}
Console.Write("Seems to be commutative");
}
结果:
Combine(0, 1) = -839320321 != 864340100 = Combine(1, 0)
所以Hash.Combine
(当前实现,.Net Core 3.1)不是可交换的