• Assuming

    class A { }

    class B : A { }

covariance is not supported for generic class.

Meaning - we cant do something like this :

MyConverter<B> x1= new MyConverter<B>();
MyConverter<A> x2= x1;  

Thats fine and understood.

From my reading - i understand that Covariance will be available:

"If you use a backing Generic Interface Being implemented on a Generic Class - so that access to the T type object instance will be available through those interfaces ".

I have just one problem.

Ive seen many examples of the "converter" class as a form of Stack .

But never understood " what if I want to use only 1 instance of B from a reference of A ? "

so Ive tried some code :

Create B object + values ---> use Generic Converter for B ---> use the covariance flow to get its A reference ---> now you can use it either as A or as B.

enter image description here

enter image description here

My question :

Is That the correct way of doing this ( for using covariance for 1 object only ) ?

p.s. The code is working and compiled ok. https://i.sstatic.net/PJ6QO.png

Ive been asking /reading a lot about this topic lately - I dive into things in order to understand them the best I can.

Edit
Report