0

I've the below code.

interface ITest<in T> where T: Message
{
    void Print(T t);
}

abstract class Message
{

}

class SimpleMessage: Message
{

}

class Test: ITest<SimpleMessage>
{
    public void Print(SimpleMessage message)
    {

    }
}

Why the below statement is throwing error?

ITest<Message> test = new Test();

Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'Test' to 'ITest<HelloAspose.Message>'. An explicit conversion exists (are you missing a cast?)

VJAI
  • 31,095
  • 22
  • 99
  • 157
  • 1
    For that cast to work you'd need Co-Variance. It would be bad if you passed any `Message` to something that expects a `SimpleMessage`. – juharr May 16 '22 at 17:46

0 Answers0