Search code examples
c#genericsmonocovariancexamarin

Covariance out issue with generic collection in an interface


I'm a bit confused as to why I'm having this error here. Since I've declared T as out, shouldn't it be OK to return its type?

public interface IWebFeed<out T> where T : BaseItem
{
    List<T> getItems();

    void loadFeed();
}

Instead I get:

The covariant type parameter T' must be invariantly valid on Shared.Service.Feed.IWebFeed.getItems()'

EDIT: So, as mbeckish pointed out, in his link, changing the List<T> to IEnumerable<T> should resolve this, but when I follow the declaration for IEnumerable, I get the following:

using System;

namespace System.Collections.Generic
{
    public interface IEnumerable<T> : IEnumerable
    {
        //
        // Methods
        //

        IEnumerator<T> GetEnumerator();
    }
}

Is this a bug in Xamarin or their PCL? The fact that T isn't out?


Solution

  • Interface variance is not yet supported in MonoTouch ( Xamarin ) and should be coming in a future version as Mono has added interface variance relatively recently.