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
>