6

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.

6
  • 1
    What does "pass an implementation of IAmA" mean? ClassB does not inherit anything from ClassA, it still has to implement every member of IAmB. Why do you have to inherit from IAmB<ClassA> instead of IAmB<IAmA> ? What is the actual problem you are trying to solve? Commented Sep 10, 2015 at 7:24
  • I updated the question. Commented Sep 10, 2015 at 7:25
  • the update didn't explain anything - it's clear from the start you want to use the concrete class - the question is why? Commented Sep 10, 2015 at 7:26
  • 1
    While you can declare T as a covariant parameter (out T), you will only be able to use it in return parameters. What is the actual problem you are trying to solve? Commented Sep 10, 2015 at 7:44
  • Well, duh - ClassB is not IAmB<IAmA>. Why would you want to treat it that way? Do you think it would be reasonable if you could just implicitly pass an object as a string argument to a method? Because that's exactly what you're doing here. Commented Sep 10, 2015 at 7:49

5 Answers 5

11

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

Sign up to request clarification or add additional context in comments.

Comments

3

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 !

Comments

2

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.

Comments

0

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
}

1 Comment

This isn't an actual answer.
0

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.

1 Comment

Not quite - covariance only allows T to be used as a return type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.