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.

Alex
  • 161
  • 1
  • 10
  • 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 instead of IAmB ? What is the actual problem you are trying to solve? – Panagiotis Kanavos Sep 10 '15 at 07:24
  • I updated the question. – Alex Sep 10 '15 at 07:25
  • the update didn't explain anything - it's clear from the start you want to use the concrete class - the question is *why*? – Panagiotis Kanavos Sep 10 '15 at 07: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*? – Panagiotis Kanavos Sep 10 '15 at 07:44
  • Well, duh - `ClassB` is *not* `IAmB`. 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. – Luaan Sep 10 '15 at 07:49
  • @PanagiotisKanavos Thanks to your question `why?` I noticed that I don't need a generic Type parameter for `IAmB` at all. – Alex Sep 10 '15 at 08:42

5 Answers5

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

Community
  • 1
  • 1
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
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 !

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
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.

lc.
  • 113,939
  • 20
  • 158
  • 187
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
}
Jiang YD
  • 3,205
  • 1
  • 14
  • 20
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.