94

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

George Stocker
  • 57,289
  • 29
  • 176
  • 237
BreakHead
  • 10,480
  • 36
  • 112
  • 165
  • 14
    I 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 Answers12

118
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>();
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • 35
    Be sure you're using System.Linq – Nestor Ledon Jul 19 '13 at 15:36
  • 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
  • Thanks for `List list = myArray.ToList();` – samir105 May 15 '17 at 06:14
48

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

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • 3
    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 :) . – Patrick Jul 07 '16 at 07:29
14

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()

fjdumont
  • 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
10

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

        object[] testArray = new object[] { "blah", "blah2" };
        List<object> testList = new List<object>(testArray);
Craig T
  • 1,031
  • 1
  • 7
  • 13
5
private List<object> ConvertArrayToList(object[] array)
{
  List<object> list = new List<object>();

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

  return list;
}
Christian
  • 4,345
  • 5
  • 42
  • 71
3

If array item and list item are same

List<object> list=myArray.ToList();
Prasanth V J
  • 1,126
  • 14
  • 32
2

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!

Null
  • 482
  • 5
  • 3
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.

Nitika Chopra
  • 1,281
  • 17
  • 22
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});
Frank
  • 11
  • 1
0

another way

List<YourClass> list = (arrayList.ToArray() as YourClass[]).ToList();
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
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);
  }
sailfish009
  • 2,561
  • 1
  • 24
  • 31
-2

This allow you to send an object:

private List<object> ConvertArrayToList(dynamic array)
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Fábio Correia
  • 962
  • 7
  • 8