Dictionary : Will calling .Keys be ordered consistently with .Values?

Suppose I have a dictionary, and call .Keys, and then .Values with no intervening operations. Will the ordering of the keys be consistent with the ordering of the values?

Put another way, will the nth key correspond to the nth value as they did before in the dictionary?

Jon Skeet
people
quotationmark

Will the ordering of the keys be consistent with the ordering of the values?

Yes, it's guaranteed by the documentation of both IDictionary<,>.Keys and IDictionary<,>.Values.

Keys documentation:

The order of the keys in the Dictionary<TKey, TValue>.KeyCollection is unspecified, but it is the same order as the associated values in the Dictionary<TKey, TValue>.ValueCollection returned by the Values property.

Values documentation:

The order of the values in the Dictionary<TKey, TValue>.ValueCollection is unspecified, but it is the same order as the associated keys in the Dictionary<TKey, TValue>.KeyCollection returned by the Keys property.

people

See more on this question at Stackoverflow