Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<int> to IEnumerable<IComparable>

I can implicitly cast an int to a IComparable. I can also cast a List or an array to a IEnumerable.

But why can't I implicitly cast a List to a IEnumerable?

I tested this with the .net framework 4.5 and Visual Studio 2012 Ultimate.

Code to test:

IComparable test1;
int t1 = 5;
test1 = t1; //OK

IEnumerable<int> test2;
List<int> t2 = new List<int>();
int[] t3 = new int[] { 5, 6 };
test2 = t2; //OK
test2 = t3; //OK

TabAlignment[] test;

IEnumerable<IComparable> test3;
test3 = t2; //error Cannot implicitly convert type 'System.Collections.Generic.List<int>' to 'System.Collections.Generic.IEnumerable<System.IComparable>'. An explicit conversion exists (are you missing a cast?)
like image 327
user886079 Avatar asked Feb 22 '13 16:02

user886079


1 Answers

Generic variance doesn't apply to value types, basically. So while you can

You'd need to box each value:

IEnumerable<IComparable> test3 = t2.Cast<IComparable>();

So while this is valid because string is a reference type:

List<string> strings = new List<string>();
IEnumerable<IComparable> comparables = strings;

... the equivalent doesn't work for List<int>, and you need to box as you go.

like image 135
Jon Skeet Avatar answered Oct 22 '22 20:10

Jon Skeet