views:

46

answers:

2

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

+3  A: 

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
How it may not be `IItem`, if i explicitly specified `where TItem : IItem`?
Shaddix
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
I get it, thanks.
Shaddix
You're welcome.
Pieter
A: 

see this SO-Post: http://stackoverflow.com/questions/4031446/non-generic-interface-as-a-synonym-for-generic-one

hacktick
yes, this was mine :) This is exactly the reason, why I can't have synonyms, and I understood this. This question is about why TItem:IItem isn't implementing IItem :)
Shaddix