Note that there are some explanatory texts on larger screens.

plurals
  1. POMonodroid generics not covariant?
    text
    copied!<p><strong>EDIT:</strong> <em>It seems to be confirmed that covariant generics are not yet supported. To make sure it makes it in the next release please email Xamarin and request this feature.</em></p> <p>The following code gives me an error (at the linq statement). Identical code works fine in Microsoft clr c# 4.0.</p> <p><strong>Error:</strong></p> <pre><code>Cannot implicitly convert type 'System.Collections.Generic.IEnumerable&lt;LinqTest.Person&gt;' to 'System.Collections.Generic.IEnumerable&lt;LinqTest.Entity&gt;'. An explicit conversion exists (are you missing a cast?) </code></pre> <p><strong>Code:</strong></p> <pre><code>[Activity(Label = "LinqTest", MainLauncher = true, Icon = "@drawable/icon")] public class Activity1 : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); var names = new List&lt;string&gt; { "Joe", "Bob", "Jim", "Jane" }; IEnumerable&lt;Entity&gt; query = from e in names select new Person() {Name = e}; SetContentView(Resource.Layout.Main); var button = FindViewById&lt;Button&gt;(Resource.Id.MyButton); button.Click += delegate { button.Text = string.Format("{0}", query.Count()); }; } } public class Entity { public string Name { get; set; } } public class Person : Entity { public string Workplace { get; set; } } public class Animal : Entity { public string FurColour { get; set; } } </code></pre> <p>Am I correct or am I missing something? If so recommendations on refactoring this?</p> <p><strong>Also, if so, consider this an official feature request for monodroid - support for covariant generic support. :)</strong></p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload