[Mono-dev] Array and IEnumerable

Michael Hutchinson m.j.hutchinson at gmail.com
Wed Jul 1 00:26:05 EDT 2009


On Tue, Jun 30, 2009 at 10:30 PM, Yopmaster<trouve.antoine at mac.com> wrote:
> I think your issue is due to another huge difference between Mono and .NET
> which leads to this kind of error. Here is an example:
>
> Let us consider "Human" and "Child" so that "Child" is derived from "Human":
>
>  class Child : Human {}
>
> If now you create the List of type
>
>  List<Child> children;
>
> In .NET, "children" will also implement the type "List<Human>":
>
>  children is List<Child>
>  >>> true
>  children is List<Human>
>  >>> true

This is incorrect. List<Subclass> cannot be cast to List<Superclass> on .NET.

Some examples like this can be solved by generic
covariance/contravariance in .NET 4.0. See
http://themonkeysgrinder.blogspot.com/2009/02/c-4-is-now.html for some
explanations. However, since System.Generic.Collections.List<T> both
accepts and returns objects of type T, I don't believe it could be
made variant.

-- 
Michael Hutchinson
http://mjhutchinson.com


More information about the Mono-devel-list mailing list