PC Review


Reply
Thread Tools Rate Thread

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

 
 
buzzweetman@gmail.com
Guest
Posts: n/a
 
      8th Aug 2006

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

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      8th Aug 2006
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 Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail 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
>



 
Reply With Quote
 
buzzweetman@gmail.com
Guest
Posts: n/a
 
      9th Aug 2006
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<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 Removed)
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail 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
> >


 
Reply With Quote
 
Larry Lard
Guest
Posts: n/a
 
      9th Aug 2006
Nicholas Paldino [.NET/C# MVP] wrote:
> 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 Removed)
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
 
Reply With Quote
 
buzzweetman@gmail.com
Guest
Posts: n/a
 
      9th Aug 2006
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.

So it seems I DO want to pass a List<string> and just create the list
in the more elegant way that Nicholas suggsted in the first place:
> > // Get the list.
> > List<string> keyNameList = 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<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 Removed)
> The address is real, but unread - please reply to the group
> For VB and C# questions - tell us which version


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      9th Aug 2006
If you want Contains, then ICollection<T> may be the interface you want -
which Dictionary<TKey,TValue>.KeyCollection happens to implement...

Marc


 
Reply With Quote
 
Larry Lard
Guest
Posts: n/a
 
      9th Aug 2006
Marc Gravell wrote:
> 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!

--
Larry Lard
(E-Mail Removed)
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
serialize dictionary<string, string> collection in .net3.5 Andy B Microsoft C# .NET 6 7th Apr 2008 12:57 PM
convert generic string list to one string gs Microsoft C# .NET 9 14th Dec 2007 08:27 PM
convert generic string list to one string GS Microsoft C# .NET 0 12th Dec 2007 07:55 PM
Create Permutations from Dictionary<string, List<string>> Assimalyst Microsoft C# .NET 2 30th Nov 2007 07:50 PM
Convert FieldInfo to Dictionary<string, string> =?Utf-8?B?V2lsc29u?= Microsoft C# .NET 7 22nd Jun 2006 11:00 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:34 PM.