How do I get the list of keys in a Dictionary?

C#ListDictionary

C# Problem Overview


I only want the Keys and not the Values of a Dictionary.

I haven't been able to get any code to do this yet. Using another array proved to be too much work as I use remove also.

How do I get a List of the Keys in a Dictionary?

C# Solutions


Solution 1 - C#

List<string> keyList = new List<string>(this.yourDictionary.Keys);

Solution 2 - C#

You should be able to just look at .Keys:

    Dictionary<string, int> data = new Dictionary<string, int>();
    data.Add("abc", 123);
    data.Add("def", 456);
    foreach (string key in data.Keys)
    {
        Console.WriteLine(key);
    }

Solution 3 - C#

Update for .NET 3.5+

To get list of all keys:

using System.Linq;

List<String> myKeys = myDict.Keys.ToList();

If you face any issues using System.Linq, see the following:

Solution 4 - C#

Marc Gravell's answer should work for you. myDictionary.Keys returns an object that implements ICollection<TKey>, IEnumerable<TKey> and their non-generic counterparts.

I just wanted to add that if you plan on accessing the value as well, you could loop through the dictionary like this (modified example):

Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("abc", 123);
data.Add("def", 456);

foreach (KeyValuePair<string, int> item in data)
{
    Console.WriteLine(item.Key + ": " + item.Value);
}

Solution 5 - C#

I can't believe all these convoluted answers. Assuming the key is of type: string (or use 'var' if you're a lazy developer): -

List<string> listOfKeys = theCollection.Keys.ToList();

Solution 6 - C#

The question is a little tricky to understand but I'm guessing that the problem is that you're trying to remove elements from the Dictionary while you iterate over the keys. I think in that case you have no choice but to use a second array.

ArrayList lList = new ArrayList(lDict.Keys);
foreach (object lKey in lList)
{
  if (<your condition here>)
  {
    lDict.Remove(lKey);
  }
}

If you can use generic lists and dictionaries instead of an ArrayList then I would, however the above should just work.

Solution 7 - C#

Or like this:

List< KeyValuePair< string, int > > theList =
    new List< KeyValuePair< string,int > >(this.yourDictionary);

for ( int i = 0; i < theList.Count; i++)
{ 
  // the key
  Console.WriteLine(theList[i].Key);
}

Solution 8 - C#

For a hybrid dictionary, I use this:

List<string> keys = new List<string>(dictionary.Count);
keys.AddRange(dictionary.Keys.Cast<string>());

Solution 9 - C#

I often used this to get the key and value inside a dictionary: (VB.Net)

 For Each kv As KeyValuePair(Of String, Integer) In layerList

 Next

(layerList is of type Dictionary(Of String, Integer))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAthiwat ChunlakhanView Question on Stackoverflow
Solution 1 - C#CamalView Answer on Stackoverflow
Solution 2 - C#Marc GravellView Answer on Stackoverflow
Solution 3 - C#premView Answer on Stackoverflow
Solution 4 - C#ThorarinView Answer on Stackoverflow
Solution 5 - C#Antony BoothView Answer on Stackoverflow
Solution 6 - C#DanView Answer on Stackoverflow
Solution 7 - C#user176829View Answer on Stackoverflow
Solution 8 - C#KosmasView Answer on Stackoverflow
Solution 9 - C#Joseph WuView Answer on Stackoverflow