98

How do I convert an Array to a List<object> in C#?

3

13 Answers 13

123
List<object> list = myArray.Cast<Object>().ToList();

If the type of the array elements is a reference type, you can leave out the .Cast<object>() since C#4 added interface co-variance i.e. an IEnumerable<SomeClass> can be treated as an IEnumerable<object>.

List<object> list = myArray.ToList<object>();
Sign up to request clarification or add additional context in comments.

3 Comments

Be sure you're using System.Linq
Does this only work in VS2015? Under VS2012: 'System.Array' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
Thanks for List<object> list = myArray.ToList<object>();
51

Use the constructor: new List<object>(myArray)

1 Comment

A nice thing about this style is that it creates and returns the list, so you can append more methods (e.g. .Contains(userName)). Useful for cramming a bunch of logic into a one-line if statement :) .
15

List<object>.AddRange(object[]) should do the trick. It will avoid all sorts of useless memory allocation. You could also use Linq, somewhat like this: object[].Cast<object>().ToList()

1 Comment

A nice thing about this approach is you are not constantly allocating new List<T> - you can Clear() and reuse an existing list.
11

The List<> constructor can accept anything which implements IEnumerable, therefore...

        object[] testArray = new object[] { "blah", "blah2" };
        List<object> testList = new List<object>(testArray);

Comments

5
private List<object> ConvertArrayToList(object[] array)
{
  List<object> list = new List<object>();

  foreach(object obj in array)
    list.add(obj);

  return list;
}

Comments

3

If array item and list item are same

List<object> list=myArray.ToList();

1 Comment

I had to add using using System.Linq; in order to have it working; not sure why.
3

Everything everyone is saying is correct so,

int[] aArray = {1,2,3};
List<int> list = aArray.OfType<int> ().ToList();

would turn aArray into a list, list. However the biggest thing that is missing from a lot of comments is that you need to have these 2 using statements at the top of your class

using System.Collections.Generic;
using System.Linq;

I hope this helps!

Comments

2

You can try this,

using System.Linq;
string[] arrString = { "A", "B", "C"};
List<string> listofString = arrString.OfType<string>().ToList();

Hope, this code helps you.

Comments

1

You can also initialize the list with an array directly:

List<int> mylist= new List<int>(new int[]{6, 1, -5, 4, -2, -3, 9});

Comments

0

another way

List<YourClass> list = (arrayList.ToArray() as YourClass[]).ToList();

Comments

0

Here is my version:

  List<object> list = new List<object>(new object[]{ "test", 0, "hello", 1, "world" });

  foreach(var x in list)
  {
      Console.WriteLine("x: {0}", x);
  }

Comments

0

Depending of the Array type.

If you have a function that returns the Array type then:

List<string> x = new List<string>{"a","b","c"};

//this is a none predefined array object.
Array _test = x.ToArray();

If you want to convert the _test back to a List<string> you can easily do it like this:

List abx = x.getValue(0) as List<string>;

If you a have predefined array object:

string[] arrString = { "A", "B", "C"};
//or
string[] arrstring = string[6];

Then you can cast the arrstring.toList();

Comments

-2

This allow you to send an object:

private List<object> ConvertArrayToList(dynamic array)

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.