Convert dictionary values into array Convert dictionary values into array arrays arrays

Convert dictionary values into array


// dict is Dictionary<string, Foo>Foo[] foos = new Foo[dict.Count];dict.Values.CopyTo(foos, 0);// or in C# 3.0:var foos = dict.Values.ToArray();


Store it in a list. It is easier;

List<Foo> arr = new List<Foo>(dict.Values);

Of course if you specifically want it in an array;

Foo[] arr = (new List<Foo>(dict.Values)).ToArray();


There is a ToArray() function on Values:

Foo[] arr = new Foo[dict.Count];    dict.Values.CopyTo(arr, 0);

But I don't think its efficient (I haven't really tried, but I guess it copies all these values to the array). Do you really need an Array? If not, I would try to pass IEnumerable:

IEnumerable<Foo> foos = dict.Values;