I've IEnumerable<object>
type variable.
IEnumerable<object> items= new object[] { 1, 2, 3 };
What's the best way to check if it's IEnumerable<int>
?
I tried
typeof(IEnumerable<int>).IsAssignableFrom(items.GetType())
typeof(IEnumerable<int>).IsInstanceOfType(items)
items is IEnumerable<int>
But, Re-Sharper complains about them all.
In my case, IEnumerable<object> items
is of type IEnumerable<int>
in most cases. And I wanted to carry out something when it's of type IEnumerable<int>
and something else for other types.
If you want to check if an IEnumerable<object>
contains only ints, you can use Enumerable.All
:
var isInts = items.All(x => x is int);
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