Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

c#

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

like image 523
BreakHead Avatar asked Feb 07 '11 14:02

BreakHead


People also ask

How do you transform an array into an object?

To convert an array to an object, use the reduce() method to iterate over the array, passing it an object as the initial value. On each iteration, assign a new key-value pair to the accumulated object and return the result. Copied! const arr = ['zero', 'one', 'two']; const obj4 = arr.


1 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>(); 
like image 58
CodesInChaos Avatar answered Sep 20 '22 23:09

CodesInChaos