我注意到这两个接口,以及几个相关的类,已经在.NET 4中添加了.它们对我来说似乎有点多余; 我已经阅读了几个关于它们的博客,但我仍然无法弄清楚它们在.NET 4之前解决了哪些棘手问题.
什么是使用IStructuralEquatable和IStructuralComparable?
面对字典中的问题.数组是否可以成为价值的关键???
Dictionary<string[], int> di = new Dictionary<string[], int>();
di.Add(new string[]
{
"1","2"
}, 1);
di.Add(new string[]
{
"2","3"
}, 2);
MessageBox.Show(di[new string[] { "2", "3" }].ToString()); // Here KeyNotFoundException occurred.
Run Code Online (Sandbox Code Playgroud)
为什么例外?
我有一本字典如下
Dictionary<ulong, Dictionary<byte[], byte[]>> Info;
Run Code Online (Sandbox Code Playgroud)
内部字典将byte []数组作为键.
我无法理解如何声明Info字典的构造函数.对于我所拥有的内部键比较ByteArrayComparer,
public class ByteArrayComparer : IEqualityComparer<byte[]>
{
public bool Equals(byte[] left, byte[] right)
{
if (left == null || right == null)
{
return left == right;
}
if (left.Length != right.Length)
{
return false;
}
for (int i = 0; i < left.Length; i++)
{
if (left[i] != right[i])
{
return false;
}
}
return true;
}
public int GetHashCode(byte[] key)
{
if (key == null)
throw new …Run Code Online (Sandbox Code Playgroud)