0

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 IEnumberable<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?

Josh
  • 12,448
  • 10
  • 74
  • 118
  • Totally out of subject, `but List getItems();` with a lower case `g` sounds too Java. Either change it to `GetItems()` or to `Items {get;}` – Federico Berasategui Feb 08 '13 at 03:26
  • Well, as I'm a Java / Objective-C programmer, playing with Xamarin / C#, that would probably be the reason :P Is that a common notation? If so, noted! – Josh Feb 08 '13 at 03:27
  • @mbeckish: I appreciate that, but I get the exact same error, changing `List` to `IEnumerable`. – Josh Feb 08 '13 at 03:34
  • @mbeckish: Please see my edit re: `IEnumerable`. – Josh Feb 08 '13 at 03:37
  • Enumerable has out keyword: http://msdn.microsoft.com/en-us/library/9eekhta0.aspx – cuongle Feb 08 '13 at 03:39
  • 1
    @Josh - looks like you are correct. See http://stackoverflow.com/q/14093332/21727 and http://forums.xamarin.com/discussion/683/does-monotouch-support-variant-generic-interfaces – mbeckish Feb 08 '13 at 03:43
  • 1
    @CuongLe I'm using Xamarin, not MS C#. – Josh Feb 08 '13 at 03:46

2 Answers2

1

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.

manojlds
  • 290,304
  • 63
  • 469
  • 417
-1

You need to change the definitionof the GetItems method to be the following...

List<BaseItem> GetItems();

This is because you are declaring a restriction on T that it must fit the shape of BaseItem and hence in order for the variance to hold you must restrict the type of items within the list. This allows you to creaet multiple lists of BaseItem classes and interchange them without the need for explicit conversions.