views:

400

answers:

4

if i have an array. can i populate a generic list from that array:

Foo[] fooList . . . (assume populated array)

// This doesn't seem to work
List<Foo> newList = new List<Foo>(fooList);
+2  A: 

You are looking for List(t).AddRange Method

kaivalya
+3  A: 

You could convert the array to a List:

string[] strings = { "hello", "world" };
IList<string> stringList = strings.ToList();
Andy White
ToList() isn't even necessary since arrays already implement IList<T>. ;)
Reed Copsey
+2  A: 

As @korki said, AddRange will work, but the code you've posted should work fine. For example, this compiles:

var i = new int[10];
var list = new List<int>(i);

Could you show us more of your code?

Matt Hamilton
A: 

Works fine here. Perhaps your array is uninitialized.

AngryHacker