我想在C#中使用布尔数组作为其键的字典。使用布尔数组作为自定义字典键
Dictionary<bool[], string>
布尔数组的固定长度为1000,并且都是相同的长度。由于数组的长度,我在散列码方面遇到了问题,而'exclusive or'的通用方法并没有那么有意义。
StackOverflow上的类似问题在GetHashCode方法中用'exclusive或'来解决。我不认为这在这方面起作用。我想用它作为:
Dictionary<bool[], string> myDict =
new Dictionary<bool[], string>(EqualityComparer);
其中EquaityComparer确实是这样的:
public class EqualityComparer : IEqualityComparer<bool[]>
{
public bool Equals(bool[] x, bool[] y)
{
return x.SequenceEqual(y);
}
public int GetHashCode(bool[] x)
{
// this part doesn't work correctly
int hc = x.GetHashCode();
return hc;
}
}
当然,所有关于布尔数组是可变的普遍关注和任何派生密钥是有关大小以性能适用于此...虽然我没有解决方案。
我觉得你需要实现你自己的,而不是为'bool []'调用默认'GetHashCode'。 – FishBasketGordo 2012-07-17 17:41:21
'return x.Intersect(y)== x;'也是不正确的。您正在比较'IEnumerable'的'实例'和布尔数组 –
2012-07-17 17:44:12
当然。我使用SequenceEqual来等待equals方法。在这里我更具体地需要帮助的哈希码。 – Vic 2012-07-17 18:04:16