Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a partilcular item from a Dictionary object with index?

Tags:

c#

How to retrive an item from an dictionary object using an Index? eg i have a dicitiory object of 10 items and i have to get the 5th keypairvalue from the dictionary?

like image 890
Kishore Kumar Avatar asked Jan 25 '26 11:01

Kishore Kumar


1 Answers

Dictionaries are unordered. If you mean "the 5th item added to the dictionary" - they don't provide this functionality.

One thing to be careful of is that in many cases Dictionary<TKey, TValue> appears to be ordered - if you just add a bunch of entries and then iterate, then under the current implementation I believe you will at least usually get back the pairs in the same order. However, it's not guaranteed, it's not meant to happen particularly - it's just a quirk of the implementation. If you delete entries and then add more, then the whole thing goes pear-shaped.

Fundamentally, if you want ordering as well as key lookups, you need to store a list as well as a dictionary.

like image 129
Jon Skeet Avatar answered Jan 28 '26 01:01

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!