Alex
Alex

Reputation: 161

C# Generics, interfaces and inheritance

I've two interfaces:

public interface IAmA
{
}

public interface IAmB<T> where T : IAmA
{
}

And two classes implementing these interfaces like this:

public class ClassA : IAmA
{
}

public class ClassB : IAmB<ClassA>
{
}

When trying to use these classes as shown:

public class Foo
{
    public void Bar()
    {
        var list = new List<IAmB<IAmA>>();
        list.Add(new ClassB());
    }
}

I get this compiler error:

cannot convert from 'ClassB' to 'IAmB<IAmA>'

I know I can make the compiler happy using:

public class ClassB : IAmB<IAmA>
{
}

But I need to be able to be the Type parameter for IAmB<> in ClassB an implementation of IAmA.

Upvotes: 6

Views: 343

Answers (5)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131364

The quick answer is that you can do what you ask by declaring the type parameter of IAmB<T> as covariant, only if the type is used as a return type:

public interface IAmB<out T> where T : IAmA
{
    T SomeMethod(string someparam);
}

out T means that you can use a more specific type than then one specified in the constraints.

You won't be able to use T as a parameter. The following won't compile:

public interface IAmB<out T> where T : IAmA
{
    void SomeMethod(T someparam);
}

From the documentation

You can use a covariant type parameter as the return value of a method that belongs to an interface, or as the return type of a delegate. You cannot use a covariant type parameter as a generic type constraint for interface methods.

This isn't a compiler quirk. Assuming you could declare a covariant method parameter, your list would end up containing some objects that couldn't handle an IAmB<IAmA> parameter - they would expect an input of ClassA or more specific. Your code would compile but fail at runtime.

Which begs the question - why do you want to use IAmB<ClassA> ?

You should think about before using this though, as there may be other, more suitable ways to address your actual problem. It's unusual to use a generic interface implementing a concrete type but trying to use it as if it were implementing another interface.

You can check the MSDN documentation's section on Covariance and Contravariance as well as Eric Lippert's an Jon Skeet's answers to this SO question: Difference between Covariance and Contravariance

Upvotes: 11

user3844214
user3844214

Reputation: 23

It can be solved using Contravariance and Covariance.

public interface IAmA
{
}

**public interface IAmB<out T> where T : IAmA
{
}**


public class ClassA : IAmA
{
}

public class ClassB : IAmB<ClassA>
{
}


public class Foo
{
    public void Bar()
    {
        var list = new List<IAmB<IAmA>>();
        **list.Add(new ClassB());**
    }
}

Now you don't get compiler error. Compiler is happy.

Upvotes: 0

Jiang YD
Jiang YD

Reputation: 3311

I just tell why this error reported.

if your IAmB has a method

public interface IAmB<T> where T : IAmA
{
    void foo(T p);
}

public class ClassB : IAmB<ClassA>
{
    void foo(ClassA p)
    {
        p.someIntField++;
    }
}

and we have another class

public class ClassC : IAmB<ClassA2>
{
    void foo(ClassA2 p)
    {
        p.someOtherIntField++;
    }
}

and we assume List<IAmB<IAmA>>.Add(T p) implement like this

IAmA mParam = xxxx;
void Add(IAmB<IAmA>> p){
    p.foo(mParam);
}

thinking all compile OK. you pass a ClassB instance to List.Add, it becomes

void Add(IAmB<IAmA>> p){
    //p is ClassB now
    p.foo(mParam);//COMPILER CAN NOT MAKE SURE mParam fit ClassB.foo
}

Upvotes: 0

lc.
lc.

Reputation: 116458

The trick is making the type constraint T on IAmB<T> covariant, with the out keyword:

public interface IAmB<out T> where T : IAmA
{
}

This allows you to use a more specific type than originally specified, in this case allowing you to assign an IAmB<ClassA> to a variable of type IAmB<IAmA>.

For more information, see the documentation.

Upvotes: 2

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

Fast answer : make the generic type covariant (see msdn) in your interface

public interface IAmB<out T> where T : IAmA
{
}

this will resolve the compiler problem.

But this won't answer the why asked by Panagiotis Kanavos !

Upvotes: 3

Related Questions