Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I assign List<int> to IEnumerable<object> in .NET 4.0

I try to do this:

IEnumerable<object> ids = new List<string>() { "0001", "0002", "0003" };

it works great!

But when I try to do this:

IEnumerable<object> intIds = new List<System.Int32>() { 1, 2, 3 };

Visual Studio tells me: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

Why is that?

like image 592
OnlyMahesh Avatar asked Nov 17 '11 22:11

OnlyMahesh


1 Answers

int is a value type and can only be boxed to object - it doesn't inherit from object. Since you're using an IEnumerable<object> anyway, this should work:

IEnumerable intIds = new List<int>() { 1, 2, 3 };
like image 100
Ry- Avatar answered Sep 28 '22 08:09

Ry-