Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from object {object[]} to List<int>

Tags:

c#

I need to convert from

object {object[]}

to

System.Collections.Generic.List<int>

Inside each element in the object array there is a object {int} element. I got this structure from a COM dll.

I was wondering how is the easiest way to do that. Thanks in advance!

like image 460
Thiago Avatar asked Dec 04 '22 14:12

Thiago


2 Answers

try

List<int> intList = objectArray.Cast<int>().ToList();
like image 165
D Stanley Avatar answered Dec 10 '22 11:12

D Stanley


List<Int> ObjToList(object[] objects)
{
    List<int> intList = new list<int>();
    foreach (object o in objects)
    {
        intList.Add((int)o);
    }
    return intList;
}

be very sure that all your objects in the array are of type int to avoid problems

like image 35
Freeman Avatar answered Dec 10 '22 10:12

Freeman