EDIT:
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.
The following code gives me an error (at the linq statement). Identical code works fine in Microsoft clr c# 4.0.
Error:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<LinqTest.Person>' to 'System.Collections.Generic.IEnumerable<LinqTest.Entity>'. An explicit conversion exists (are you missing a cast?)
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<string> {
"Joe",
"Bob",
"Jim",
"Jane"
};
IEnumerable<Entity> query =
from e in names
select new Person() {Name = e};
SetContentView(Resource.Layout.Main);
var button = FindViewById<Button>(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; }
}
Am I correct or am I missing something? If so recommendations on refactoring this?
Also, if so, consider this an official feature request for monodroid – support for covariant generic support. 🙂
Thanks.
Neither MonoTouch nor Mono for Android has support for covariance right now. It will be supported in a future release (probably some time in the second half of this year).