我想有多個字符串鍵的數據結構。爲此,我嘗試使用string []元素創建一個Dictionary。但這樣做的containsKey似乎沒有如我所料的工作:字典的containsKey似乎並不與字符串工作[]鍵
Dictionary<string[], int> aaa = new Dictionary<string[], int>();
int aaaCount = 0;
aaa.Add(new string[] { string1, string2 }, aaaCount++);
if (!aaa.ContainsKey(new string[] { string1, string2 }))
{
aaa.Add(new string[] { string1, string2 }, aaaCount++);
}
我看到有在AAA級兩個項目的代碼,而我期待只有一個以上的執行之後。這是預期的行爲?我如何確保字典中沒有重複的條目?
注:我嘗試同樣與列表以及(列表,結果是相同的 - Contains方法並沒有真正與串[]工作)
請勿使用XOR;相反,使用'res * 23 + item.GetHashCode()'之類的東西。 http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode – SLaks
嗯,我提到的實現相當幼稚:)無論如何謝謝爲了您的評論!我更新了答案,以免混淆任何人。 –