what is the most efficient way of turning the list of values of a dictionary into an array
for example, if i have:
Dictionary where key is string and value is Foo
i want to get Foo[]
I am using VS 2005, C# 2.0
what is the most efficient way of turning the list of values of a dictionary into an array
for example, if i have:
Dictionary where key is string and value is Foo
i want to get Foo[]
I am using VS 2005, C# 2.0
// 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();
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;