Trying to extract a list of keys from a .NET Dictionary

If you need a true list:

List<string> myKeys = new List<string>(myDict.Keys);

Using LINQ you can do the following...

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

However depending on what your goal is with the keys (selective enumeration etc) it might make more sense to work with the key collection and not convert to a list.


KeyCollection implements the IEnumerable interface.

You can use an extension method to convert it to a list.

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

Or use a different constructor:

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

Yes, you can try - IEnumerable<String> myKeys = myDict.Keys;

Always a good idea to use IEnumerable (a more generic type).