0

Lets say I have a class, which implements a generic interface public interface IItem {}

public interface IStuff<out TItem> where TItem : IItem
{
    TItem FavoriteItem { get; }
}

public class MyStuff<TItem> : IStuff<TItem> where TItem : IItem
{
    public TItem FavoriteItem
    {
        get { throw new NotImplementedException(); }
    }
}

I have also one non-generic interface

public interface IFavoriteItem
{
    IItem FavoriteItem { get; }
}

I'd like to make MyStuff class implement this IFavoriteItem interface. Since TItem implements IItem it seems for me, that public TItem FavoriteItem property is implementing IFavoriteItem already.

But compiler doesn't think so, and it wants me to declare a separate IItem IFavoriteItem.FavoriteItem in MyClass. Why is it so? Isn't c# covariance the thing that should play here and solve my problem?

Thanks

Shaddix
  • 5,901
  • 8
  • 45
  • 86

1 Answers1

3

The reason for this is that FavoriteItem of IFavoriteItem may not be IItem, where on the IFavoriteItem, it must be an IItem. The only way to solve this is by:

IItem IFavoriteItem.FavoriteItem
{
    get { return FavoriteItem; }
}

This will simply shortcut the call to your TItem implementation.

A good example of where this is used quite often is with the implementation of IEnumerable<>. These often look like this:

public class MyEnumerable : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
  • How it may not be `IItem`, if i explicitly specified `where TItem : IItem`? – Shaddix Oct 29 '10 at 07:34
  • Because it can be an inherited interface of, or a `class` implementation of `IItem`. The `where` does not specify that it must be **exactly** `IItem`, but instead that it must be `typeof(IItem).IsAssignableFrom()`. However, your `IFavoriteItem` has the exact `IItem` type specified. – Pieter van Ginkel Oct 29 '10 at 07:36
  • Not sure if this is related, but why is it that List implements both IList and IList, but doesn't have to implement Add(object item), it only implements Add(T item) ? – Roly Feb 24 '12 at 15:36
  • Nevermind, found the answer: http://stackoverflow.com/questions/4388782/how-listt-does-not-implement-addobject-value – Roly Feb 24 '12 at 15:40