What's the easiest way, given a Type
object, to test to see whether it is actually a list of objects? I.e. Array or IEnumerable/IEnumerable<>.
Given an object or an IList<T>, you can use the following code to check whether the object is an array. IList<string> someIds = new string[0]; if (someIds is Array) { // yes! } IList<string> someIds = new List<string>(0); if (someIds is Array) { // nop! }
IEnumerable is a behavior while Array is a data structure(Contiguous collection of elements with fixed size, facilitating accessing elements by indexes) .
IEnumerable is conceptually faster than List because of the deferred execution. Deferred execution makes IEnumerable faster because it only gets the data when needed. Contrary to Lists having the data in-memory all the time.
Check typeof(IEnumerable).IsAssignableFrom(type)
.
Every collection type, including arrays and IEnumerable<T>
, implements IEnumerable
.
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