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

- 57,289
- 29
- 176
- 237

- 10,480
- 36
- 112
- 165
-
14I don't see what's wrong with this question.. -5? Really? – Filip Ekberg Feb 07 '11 at 14:47
-
6+1 because it is a simple, effective question for which I did not find a duplicate on SO. (Downvoters should comment or reconsider.) http://stackoverflow.com/questions/1003841/how-do-i-move-the-turtle-in-logo http://blog.stackoverflow.com/2009/06/podcast-58/ – JYelton Feb 07 '11 at 16:08
-
Possible duplicate of [Conversion of System.Array to List](http://stackoverflow.com/questions/1603170/conversion-of-system-array-to-list) – Jim Fell Jun 01 '16 at 20:27
12 Answers
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>();

- 106,488
- 23
- 218
- 262
-
35
-
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?) – ajeh Jul 12 '16 at 14:16
-
Use the constructor: new List<object>(myArray)

- 33,626
- 4
- 73
- 96
-
3A 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 :) . – Patrick Jul 07 '16 at 07:29
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,517
- 1
- 9
- 22
-
A nice thing about *this* approach is you are not constantly allocating new `List
` - you can `Clear()` and reuse an existing list. – Engineer May 22 '22 at 21:33
The List<> constructor can accept anything which implements IEnumerable, therefore...
object[] testArray = new object[] { "blah", "blah2" };
List<object> testList = new List<object>(testArray);

- 1,031
- 1
- 7
- 13
private List<object> ConvertArrayToList(object[] array)
{
List<object> list = new List<object>();
foreach(object obj in array)
list.add(obj);
return list;
}

- 4,345
- 5
- 42
- 71
If array item and list item are same
List<object> list=myArray.ToList();

- 1,126
- 14
- 32
-
I had to add using using System.Linq; in order to have it working; not sure why. – user3752281 Mar 14 '17 at 13:33
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!

- 482
- 5
- 3
You can try this,
using System.Linq;
string[] arrString = { "A", "B", "C"};
List<string> listofString = arrString.OfType<string>().ToList();
Hope, this code helps you.

- 1,281
- 17
- 22
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});

- 11
- 1
another way
List<YourClass> list = (arrayList.ToArray() as YourClass[]).ToList();

- 106,488
- 23
- 218
- 262

- 91
- 7
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);
}

- 2,561
- 1
- 24
- 31
This allow you to send an object:
private List<object> ConvertArrayToList(dynamic array)

- 106,488
- 23
- 218
- 262

- 962
- 7
- 8