[Mono-dev] Array and IEnumerable

Trouve Antoine trouve.antoine at mac.com
Wed Jul 1 01:13:43 EDT 2009


On 平成 21/07/01, at 13:55, Trouve Antoine wrote:

> 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.
I have missed something and written too fast: this projects works in  
the case I use my old interfaces (e.g. IList instead of List)
It the point explained in your link as "variance" right ? By the way I  
remember it was not supported by Mono 1.3 or something.
I tested and it works with gmcs 2.4 (and 1.9 since I've just tested).

> Thank you for your link about covariance/contravariance, seems  
> interesting...




More information about the Mono-devel-list mailing list