0

I have the following code

interface IEncryption{}
class EncryptionA : IEncryption {}
class EncryptionHandler<T> where T: IEncryption{}
class EncryptionMenu
{
    public EncryptionMenu(EncryptionHandler<IEncryption> encryption){}
}

void main()
{
   EncryptionMenu encryptionMenu = new EncryptionMenu(new EncryptionHandler<EncryptionA>()); // Error
}

I get

"Argument type 'EncryptionHandler< EncryptionA>' is not assignable to parameter type 'EncryptionHandler< IEncryption>'"

and I cant understand why it cant convert it even though EncryptionA implements IEncryption and is supposed to cast it. I tried to make the EncryptionMenu generic too but it makes the same error bubble upwards and complicates the code.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

0

You have to mark T as out: class EncryptionHandler where T: IEncryption{}

its about c# co/contravariance

Aik
  • 3,528
  • 3
  • 19
  • 20