Convariance (covariant): a reference to a derived type can be assigned to a base class variable.
1 classAnimal2 {3 Public intNumberoflegs =4;4 }5 classDog:animal6 {7 8 }9 class ProgramTen { One Static voidMain () A { -Animal A1 =NewAnimal (); -Animal A2 =NewDog ();//Assign a reference to a derived class to the base class theConsole.WriteLine ("{0}", A2. Numberoflegs); - Console.read (); - } -}
Contravariance (contravariant): a delegate's reference to a stack variable that assigns a value to another delegate type. The constraint type of two of these delegate types is an inheritance relationship! Inverter keyword in
classAnimal { Public intNumberoflegs =4; } classDog:animal {}classProgram {Delegate voidaction<inchT> (T a);//In contravariant keywords Static voidActonanimal (Animal a) {Console.WriteLine (a.numberoflegs); } Static voidMain () {Action<Animal> Act1 = Actonanimal;//Create a delegate variable with a method parameter of type animal and assign a valueaction<dog> dog1 = Act1;//assign a variable to a delegate variable with a method parameter of type dog, where animal is the base class for dog! Dog1 (NewDog ()); } }
Convariance, contravariance, invariance