我有一个字典数据类型.我的问题是,Dictionary.Keys.ToList()[i]总是对应于Dictionary.Values.ToList()[i]?也就是说,以下测试总是会通过
public void DictionaryTest(int i,Dictionary<U,T> dict) { var key = dict.Keys.ToList()[i]; Assert.AreEqual(dict[key],dict.Values.ToList()[i]); }
解决方法
检查
Dictionary.Keys Property的MSDN条目:
The order of the keys in the Dictionary.KeyCollection is unspecified,but it is the same order as the associated values in the Dictionary.ValueCollection returned by the Values property.