Reputation: 170
is it possible to get a specific element from the dictionary? For example, I have a dictionary like this and I would like to get the third element as an integer, how can I do that?
public Dictionary<int, char> CodesOfLetters = new Dictionary<int, char>()
{
{260,'Ą'},
{261,'ą'},
{280,'Ę'},
{281,'ę'},
{362,'Ū'},
{363,'ū'},
{268,'Č'},
{352,'Š'},
{381,'Ž'},
{269,'č'},
{353,'š'},
{382,'ž'},
{278,'Ė'},
{279,'ė'},
{302,'Į'},
{303,'į'},
{370,'Ų'},
{371,'ų'}
};
Upvotes: 0
Views: 584
Reputation: 136074
Sure, a Dictionary<int,char>
is also an IEnumerable<KeyValuePair<int,char>>
so you can use:
var anElement = CodesOfLetters.ElementAt(2);
(as long as you have using System.Linq
at the top of your file.)
But be aware, the order of a dictionary is not guaranteed - if you care about the order of things, you probably dont want a dictionary to start with.
Edit. You comment that what you want is 280
- you can also treat the Keys
as an IEnumerable<int>
so you could do:
var thirdKey = CodesOfLetters.Keys.ElementAt(2);
with the same caveat as above. There's no guarantee the order of the keys.
If you want to keep track of the order you add items to your list, but also be able to find values by key, you could create a class that wraps both a List<int>
for the ordered list of keys, and a Dictionary<int,char>
to be able to quickly look up values by key.
Upvotes: 1