17

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);
1
  • 4
    That code definitely works. What is happening when you attempt it? Commented Apr 1, 2009 at 2:00

3 Answers 3

28

You could convert the array to a List:

string[] strings = { "hello", "world" };
IList<string> stringList = strings.ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

ToList() isn't even necessary since arrays already implement IList<T>. ;)
9

You are looking for List(t).AddRange Method

Comments

6

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.