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