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
You can use Cast<object>() call followed by ToArray():
object[] objArray = ListColl.Cast<object>().ToArray();
Method Syntax:
object[] objArray = ListColl.Cast<object>().ToArray();
Query Syntax:
var objArray = (from item in ListColl select item as object).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With