467,915 Members | 1,740 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

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


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
  • viewed: 45559
Share:
6 Replies
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
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
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
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
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
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.

Similar topics

4 posts views Thread by Mark Rae | last post: by
7 posts views Thread by Andrew Robinson | last post: by
5 posts views Thread by David Longnecker | last post: by
44 posts views Thread by Zytan | last post: by
5 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
5 posts views Thread by hanna88 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.