As vcsjones points out, for a standard dictionary, the Keys
and Values
collections will be in the same order. However, if you want a method that will create key and value arrays that will always be in the same order, for any implementation of IDictionary<TKey, TValue>
, you could do something like this:
var keyArray = new TKey[dict.Count];
var valueArray = new TValue[dict.Count];
var i = 0;
foreach (var keyAndVal in dict)
{
keyArray[i] = keyAndVal.Key;
valueArray[i] = keyAndVal.Value;
i++;
}