I have an array of objects like:
object[]
All elements of the array if compatible with a type, that I will call
MyClass
How can I convert this object[] to a List ?
>Solution :
You can use LINQ’s Cast and OfType methods, depending on what you want to happen if your array contains an element that is not of type MyClass.
If you want to throw an exception upon encountering an invalid element, use:
var myList = myArray.Cast<MyClass>().ToList();
If you want to silently ignore all items that are not MyClass, use:
var myList = myArray.OfType<MyClass>().ToList();