[mono-android] What´s wrong with this piece of code?

Jay R. Wren jrwren at xmtp.net
Fri Mar 11 08:16:23 EST 2011


Morten is right.

IList<T> is not covariant in T.

Interfaces that were updated when 4.0 shipped can be found here :
http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx

However, I think this should have generated a compiler error. I'm 90%
sure that CSC.EXE would not compile that code.
--
Jay

On 3/11/2011 7:53 AM, Morten Nilsen wrote:
>
> Hi
>
>  
>
> I suspect this might be caused by your use of IList/IDictionary for
> variables..
>
> I’m not sure a List<Dictionary<string,object>> would accept an object
> of type IDictionary<string,object>..
>
> What if, one day, the curGroupMap is changed to be an instance of
> MusicalDictionary<string,object>?
>
>  
>
> You either need to do groupData.Add((Dictionary<string,
> object>)curGroupMap) or stop casting everything into interfaces, I
> believe.
>
> If you really do want to have interfaces rather than classes here,
> your list needs to agree with that.
>
> Not everything has to be an interface, by the way – especially not
> internally in a method.
>
>  
>
>   -- mvh, Morten
>
>  
>
>  
>
> *From:*monodroid-bounces at lists.ximian.com
> [mailto:monodroid-bounces at lists.ximian.com] *On Behalf Of *Carlo Bolz
> *Sent:* Friday, March 11, 2011 1:36 PM
> *To:* Monodroid at lists.ximian.com
> *Subject:* [mono-android] What´s wrong with this piece of code?
>
>  
>
> Hello,
>
>  
>
> what´s wrong with this piece of code:
>
>  
>
>             IList<IDictionary<string, object>> groupData = new
> List<Dictionary<string, object>>() as IList<IDictionary<string, object>>;
>
>             IList<IList<IDictionary<string, object>>> childData = new
> List<List<Dictionary<string, object>>>() as
> IList<IList<IDictionary<string, object>>>;
>
>  
>
>             for (int i = 0; i < 20; i++)
>
>             {
>
>                 IDictionary<string, object> curGroupMap = new
> Dictionary<string, object>();
>
>                 curGroupMap.Add(NAME, "Group " + i.ToString());
>
>                 curGroupMap.Add(ISEVEN, (i % 2 == 0) ? "This group is
> even" : "This group is odd");
>
>                 groupData.Add(curGroupMap); // < the app dies here
>
>  
>
> Any suggestions?
>
>
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110311/c2d63205/attachment-0001.html 


More information about the Monodroid mailing list