Covariance Without Generics
A few days ago I asked why you can't treat IList<String> as IList<Object>, Wesner Moise commented with the explanation, and Mitch Denny has a post with a fuller explanation. One interesting thing that came up via a comment from Tomer Gabel was that the following code is legal:
static void Main(string[] args)
{
object[] array = GetList();
array[2] = 1;
}
public static object[] GetList()
{
return new string[10];
}
This suffer from exactly the same problem that you get with generics. In fact, while the code compiles, trying to run it results in ArrayTypeMismatchException. I never dug deep enough into the runtime so I could tell you how arrays are implemented, but I think that they are just getting special threatment from the runtime. Any Rotor hackers out there that can comment on that?
Comments
Comment preview