By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

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

P: n/a

Many times I have a Dictionary<string, SomeTypeand 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<stringkeyNameList = 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

Aug 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Buzz,

You will want to do this:

// Get the list.
List<stringkeyNameList = 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<stringand 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<stringis the
better choice.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<bu*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>
Many times I have a Dictionary<string, SomeTypeand 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<stringkeyNameList = 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

Aug 8 '06 #2

P: n/a
Thank you Nicholas.
Great advice. I'll be using your IEnumerable tip also.

Buzz

Nicholas Paldino [.NET/C# MVP] wrote:
Buzz,

You will want to do this:

// Get the list.
List<stringkeyNameList = 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<stringand 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<stringis the
better choice.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<bu*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

Many times I have a Dictionary<string, SomeTypeand 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<stringkeyNameList = 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
Aug 9 '06 #3

P: n/a
Nicholas Paldino [.NET/C# MVP] wrote:
Buzz,

You will want to do this:

// Get the list.
List<stringkeyNameList = 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<stringand 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<stringis the
better choice.
If you just want an IEnumerable<K>, then Dictionary<K,V>.Keys is a
Dictionary.KeyCollection<K>, which implements IEnumerable<Kalready.

static void Main(string[] args)
{

Dictionary<string, intdict = 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<stringlist)
{
foreach (string s in list)
{
Console.WriteLine(s);
}
}
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 9 '06 #4

P: n/a
I was passing List<stringbut 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.

So it seems I DO want to pass a List<stringand just create the list
in the more elegant way that Nicholas suggsted in the first place:
// Get the list.
List<stringkeyNameList = new List<string>(this.myDictionary.Keys);
Unless there is yet more that I am missing.
Buzz

Larry Lard wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
Buzz,

You will want to do this:

// Get the list.
List<stringkeyNameList = 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<stringand 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<stringis the
better choice.

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

static void Main(string[] args)
{

Dictionary<string, intdict = 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<stringlist)
{
foreach (string s in list)
{
Console.WriteLine(s);
}
}
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 9 '06 #5

P: n/a
If you want Contains, then ICollection<Tmay be the interface you want -
which Dictionary<TKey,TValue>.KeyCollection happens to implement...

Marc
Aug 9 '06 #6

P: n/a
Marc Gravell wrote:
If you want Contains, then ICollection<Tmay 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!

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.