我对Dictionary
的实现细节有疑问假设以下代码:
private IDictionary<Int64, Int32> _data = new Dictionary<Int64, Int32>();
private void Foo()
{
Int64[] keys = _data.Keys.ToArray<Int64>();
Int32[] vals = _data.Values.ToArray<Int32>();
// Further code goes here
}
我现在可以假设
_dict[keys[i]] == vals[i]
每个i in [0 .. _data.Length-1]
,或两个数组的顺序可能不相关?
答案 0 :(得分:3)
为什么不这样做?
private void Foo()
{
Int64[] keys = _data.Keys.ToArray<Int64>();
Int32[] vals = _data.Keys.Select(k => _data[k]).ToArray<Int32>();
// Further code goes here
}
它仍然是O(n)时间,如果将来.NET发生变化,它将不会中断。