Bharathi Bharathi -4 years ago 127
C# Question

Convert List<double> into object[]

I have some dynamic list collection and this is added on run time. I want to convert those ListColl in to array of object. The below is my code

List<double> ListColl = new List<double>();
// This is added dynamically and finally want to convert this list into object array
values.Add(10);
values.Add(40);
values.Add(20);


Expected output is like this

object[] objArray = new object[] { 10, 40, 20 };


I used
object[] objArray = ListColl.ToArray()
. But this is throwing error.

How to convert this list collection in to array of object

Answer Source

You can use Cast<object>() call followed by ToArray():

object[] objArray = ListColl.Cast<object>().ToArray();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download