Does the Enumerator of a Dictionary return key value pairs in the order they were added?

前端 未结 7 1433
暖寄归人
暖寄归人 2020-11-29 11:02

I understand that a dictionary is not an ordered collection and one should not depend on the order of insertion and retrieval in a dictionary.

However, this is what

相关标签:
7条回答
  • 2020-11-29 11:53

    It is by design that the Dictionary<TKey,TValue> is not an ordered structure as it is intended to be used primarily more for key-based access.

    If you have the need to retrieve items in a specific order, you should take a look at the Sorted Dictionary<TKey, TValue>, which takes a Comparer<T> that will be used to sort the keys in the Sorted Dictionary<TKey, TValue>.

    0 讨论(0)
提交回复
热议问题