218

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?

Jee
  • 969
  • 1
  • 7
  • 26
Athiwat Chunlakhan
  • 7,589
  • 14
  • 44
  • 72

9 Answers9

403

Use the Dictionary<TKey,TValue>.Keys property:

List<string> keyList = new List<string>(this.yourDictionary.Keys);
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Camal
  • 4,194
  • 1
  • 19
  • 11
  • 7
    is it necessary to use "this." – GorvGoyl Sep 30 '15 at 07:45
  • 20
    @JerryGoyal No, it is not necessary. It is simply used to clear confusion about whether `yourDictionary` is part of the object, derived in the function or the name a parameter. – bcdan Nov 05 '15 at 12:09
  • 3
    A more complete answer would be to not assume Key type being a string. var keyList = yourDictionary.Keys.ToList(); Or if you want to go nuts and not use var or Linq: - Type keyType = yourDictionary.GetType().GetGenericArguments()[0]; Type listType = typeof(List<>).MakeGenericType(keyType); IList keyList = (IList)Activator.CreateInstance(listType); keyList.AddRange(yourDictionary.Keys); – Antony Booth May 22 '19 at 18:47
83

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);
    }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
74

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:

KyleMit
  • 30,350
  • 66
  • 462
  • 664
prem
  • 3,348
  • 1
  • 25
  • 57
  • 16
    Please don't forget: `using System.Linq;` I need to know which answers to ignore. Sorry :) – Bitterblue Jun 06 '16 at 11:17
  • 2
    Thank you @Bitterblue. I couldn't understand why `.ToList()` was throwing an error when I had used it so many other times, so I came here looking for an answer and I realized the file I was working in didn't have `using System.Linq` :) – Drew Sep 16 '16 at 20:27
  • 1
    does not work: `Dictionary.KeyCollection' does not contain a definition for 'ToList'` – sakra Apr 10 '17 at 09:47
  • doesn't work indeed. use the accepted answer instead. – aleck Mar 27 '19 at 00:22
  • 1
    @aleck It works great. You might not have used System.Linq as suggested by Bitterblue or .Net framework version you are using does not support it. It was introduced in .Net framework 3.5, See this [link](https://stackoverflow.com/a/24659958). I am updating the answer to use System.Linq before using this method. – prem Mar 27 '19 at 06:15
  • @prem I see, it's .net core 2.1 here, so that explains. p.s. adding using System.Linq; did help. // all that msft stuff is such a mess. – aleck Mar 28 '19 at 02:20
14

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);
}
Thorarin
  • 47,289
  • 11
  • 75
  • 111
6

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();
Antony Booth
  • 413
  • 4
  • 5
  • What do you mean it doesn't work? A Dictionary has a property of keys that is of type KeyCollection, that implements ICollection that has a ToList<>() extension method. Add System.Linq to your 'using' statements and you should be good. `using System.linq;` – Antony Booth Apr 08 '19 at 03:45
2

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.

Dan
  • 7,446
  • 6
  • 32
  • 46
0

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);
}
sth
  • 222,467
  • 53
  • 283
  • 367
  • does this create a copy of the keys? (so that you can safely enumerate) – mcmillab Dec 20 '12 at 21:28
  • That's rewriting the .ToList() linq extension. Also, a List of KeyValuePair is a Dictionary. The original request was for a list of Keys only. Using a loop to populate is unnecessary as List has a constructor that takes a collection and also has an AddRange() method to add to an existing List. See the accepted answer. – Antony Booth May 22 '19 at 18:28
0

For a hybrid dictionary, I use this:

List<string> keys = new List<string>(dictionary.Count);
keys.AddRange(dictionary.Keys.Cast<string>());
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kosmas
  • 353
  • 4
  • 11
-2

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))

Joseph Wu
  • 4,786
  • 1
  • 21
  • 19