leora leora - 1 year ago 96
C# Question

convert dictionary.keyscollection to array of strings

i have a

Dictionary<string, List<Order>>
and i want to have the list of keys in an array. But when i choose

string[] keys = dictionary.Keys;

this doesn't compile.

how do i convert KeysCollection to array of strings.

Answer Source

Assuming you're using .NET 3.5 or later (using System.Linq;):

string[] keys = dictionary.Keys.ToArray();

Otherwise, you will have to use the CopyTo method, or use a loop :

string[] keys = new string[dictionary.Keys.Count];
dictionary.Keys.CopyTo(keys, 0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download