Convert Dictionary<string,SomeType> keys to List<string>

  • Thread starter Thread starter buzzweetman
  • Start date Start date
B

buzzweetman

Many times I have a Dictionary<string, SomeType> and need to get the
list of keys out of it as a List<string>, to pass to a another method
that expects a List<string>.

I often do the following:

<BEGIN CODE>
List<string> keyNameList = new List<string>();

foreach (string keyName in this.myDictionary.Keys)
{
keyNameList.Add(keyName);
}

someObject.someMethod(keyNameList);
<END CODE>

But it seems like there should be something built-in to give me a List
of the keys.
Am I missing something, or am I doing it the best way?

Thanks
Buzz
 
Buzz,

You will want to do this:

// Get the list.
List<string> keyNameList = new List<string>(this.myDictionary.Keys);

That will populate the list for you with the keys in the dictionary.

If your method is not going to modify the list, then you are better off
taking a parameter of IEnumerable<string> and not List<string>. A list is
desirable when you have to make modifications to the list, but if you are
just iterating through the list, without having to make changes to the list
(but maybe to the members of the list), then IEnumerable<string> is the
better choice.

Hope this helps.
 
Thank you Nicholas.
Great advice. I'll be using your IEnumerable tip also.

Buzz
Buzz,

You will want to do this:

// Get the list.
List<string> keyNameList = new List<string>(this.myDictionary.Keys);

That will populate the list for you with the keys in the dictionary.

If your method is not going to modify the list, then you are better off
taking a parameter of IEnumerable<string> and not List<string>. A list is
desirable when you have to make modifications to the list, but if you are
just iterating through the list, without having to make changes to the list
(but maybe to the members of the list), then IEnumerable<string> is the
better choice.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Many times I have a Dictionary<string, SomeType> and need to get the
list of keys out of it as a List<string>, to pass to a another method
that expects a List<string>.

I often do the following:

<BEGIN CODE>
List<string> keyNameList = new List<string>();

foreach (string keyName in this.myDictionary.Keys)
{
keyNameList.Add(keyName);
}

someObject.someMethod(keyNameList);
<END CODE>

But it seems like there should be something built-in to give me a List
of the keys.
Am I missing something, or am I doing it the best way?

Thanks
Buzz
 
Nicholas said:
Buzz,

You will want to do this:

// Get the list.
List<string> keyNameList = new List<string>(this.myDictionary.Keys);

That will populate the list for you with the keys in the dictionary.

If your method is not going to modify the list, then you are better off
taking a parameter of IEnumerable<string> and not List<string>. A list is
desirable when you have to make modifications to the list, but if you are
just iterating through the list, without having to make changes to the list
(but maybe to the members of the list), then IEnumerable<string> is the
better choice.

If you just want an IEnumerable<K>, then Dictionary<K,V>.Keys is a
Dictionary.KeyCollection<K>, which implements IEnumerable<K> already.

static void Main(string[] args)
{

Dictionary<string, int> dict = new Dictionary<string, int>();

dict.Add("foo", 1);
dict.Add("bar", 2);
dict.Add("pop", 3);

ListStrings(dict.Keys);

Console.ReadLine();
}

static void ListStrings(IEnumerable<string> list)
{
foreach (string s in list)
{
Console.WriteLine(s);
}
}
 
I was passing List<string> but now I've changed to IEnumerable<string>.
But now I hit a new ugliness...
I used to take that List and use the Contains method. Now with
IEnumerable I don;t have Contains. So it looks like I'll have to
foreach through... which seems kind of lame.


Unless there is yet more that I am missing.
Buzz

Larry said:
Nicholas said:
Buzz,

You will want to do this:

// Get the list.
List<string> keyNameList = new List<string>(this.myDictionary.Keys);

That will populate the list for you with the keys in the dictionary.

If your method is not going to modify the list, then you are better off
taking a parameter of IEnumerable<string> and not List<string>. A list is
desirable when you have to make modifications to the list, but if you are
just iterating through the list, without having to make changes to the list
(but maybe to the members of the list), then IEnumerable<string> is the
better choice.

If you just want an IEnumerable<K>, then Dictionary<K,V>.Keys is a
Dictionary.KeyCollection<K>, which implements IEnumerable<K> already.

static void Main(string[] args)
{

Dictionary<string, int> dict = new Dictionary<string, int>();

dict.Add("foo", 1);
dict.Add("bar", 2);
dict.Add("pop", 3);

ListStrings(dict.Keys);

Console.ReadLine();
}

static void ListStrings(IEnumerable<string> list)
{
foreach (string s in list)
{
Console.WriteLine(s);
}
}


--
Larry Lard
(e-mail address removed)
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
 
If you want Contains, then ICollection<T> may be the interface you want -
which Dictionary<TKey,TValue>.KeyCollection happens to implement...

Marc
 
Marc said:
If you want Contains, then ICollection<T> may be the interface you want -
which Dictionary<TKey,TValue>.KeyCollection happens to implement...

And if *all* we want to do is to see whether there is an entry with a
particular TKey, we just have to use Dictionary<,>.ContainsKey!

I love System.Collections.Generic!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top