0

Here is what I have:

public interface IComponent<TInput> where TInput : Input
{
    ...
}
public class SpecificComponent : IComponent<SpecificInput>
{
    ...
}
public class SpecificInput : Input
{
    ...
}
public class Configuration
{
    var Components = new List<IComponent<Input>>();
    IComponent<SpecificInput> newComponent = new SpecificComponent();
    Components.Add(newComponent);
}

This code will not compile. I get the error message:

Argument 1: cannot convert from 'IComponent<SpecificInput>' to 'IComponent<Input>'

I'm not sure why this is, since SpecificInput inherits from Input, but I assume I am just missing something simple here.

Any ideas?

  • EDIT * This is slightly different from Marked duplicate because the solution there does not entirely work for me and the code still creates a compiler warning because it doesn't recognize the inheritance on nested type parameters. That solution doesn't provide for that. And that is the root of my problem. I can't just convert them as shown in that example.
jpaull
  • 278
  • 1
  • 6
  • 17
  • 1
    `SpecificInput` is inherited from `Input`, but `IComponent` is **not** inherited from `IComponent`, it's a _different_ type. This would only work if `IComponent` was co-variant in `TInput`, but the name `TInput` suggests that this is rather contra- or invariant. – René Vogt Aug 25 '17 at 14:22
  • Thanks. I figured it out a short while ago but hadn't gotten a chance to post back here. I changed the type on the SpecificComponent from a SpecificInput to an Input and it compiled. – jpaull Aug 25 '17 at 16:53

0 Answers0