声明内部字典比较器C#

0 c# dictionary

我有一本字典如下

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 ArgumentNullException("key");
            int sum = 0;
            foreach (byte cur in key)
            {
                sum += cur;
            }
            return sum;
  }
}
Run Code Online (Sandbox Code Playgroud)

我从SO 这里拿到了

请指教

Jon*_*eet 6

比较器的规范不会Info直接作为初始化的一部分- 当你创建一个值放入外部字典时.例如:

// It's stateless, so let's just use one of them.
private static readonly IEqualityComparer<byte[]> ByteArrayComparerInstance
    = new ByteArrayComparer();

Dictionary<ulong, Dictionary<byte[], byte[]>> Info
    = new Dictionary<ulong, Dictionary<byte[], byte[]>();

....

...
Dictionary<byte[], byte[]> valueMap;

if (!Info.TryGetValue(key, out valueMap))
{
    valueMap = new Dictionary<byte[], byte[]>(ByteArrayComparerInstance);
    Info[key] = valueMap;
}
...
Run Code Online (Sandbox Code Playgroud)