[Mono-dev] Array and IEnumerable
Trouve Antoine
trouve.antoine at mac.com
Wed Jul 1 00:55:09 EDT 2009
On 平成 21/07/01, at 13:26, Michael Hutchinson wrote:
> 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
Ho, you're right. I think this behaviour has changed since .NET 2.0.
I've just checked and one of my old project do not compile anymore.
... or I might have missed something.
Thank you for your link about covariance/contravariance, seems
interesting...
More information about the Mono-devel-list
mailing list