/* * Covariance and contravariance means convertable * by implict reference conversion (Albahari). * */ public void ConvarianceExample() { /* * Covariance in generic means that a data-type (interface * or class) with a base type-parameter can hold data-type * (base or subclass) with a child type-parameter. * * I.e. * Basically covariance just means that, after marking the * iterfaces type-parameter with the 'out' keyword, the * compiler will check if the data-type of the variable is * parent data-type of the value, and also check if the generic * type-parameter is also the parent type-parameter of the * values generic. * * E.g. * IEnumrable<IExample1> covariance1 = new List<InterfaceExampleClass>(); * IEnumrable is the base of List. * IExample1 is the base of InterfaceExampleClass. * * */ /* * ICoveriance has more notes. * */ ICovariance <IExample1> covariance1 = new CovarianceAndContravarianceImpl <InterfaceExampleClass>(); }
public void ContravarianceExample() { /* * Contravariance is the opposite of covariance, which co- * nverts a parent type-parameter to a subclass data-type * with a sublclass generic type-parameter. * * I.e. * Basically covariance just means that, after marking the * interfaces type-parameter with the 'in' keyword, the co- * mpiler will check if the generic type-parameter is the * child type-parameter of the values generic. * * */ /* * IContraveriance has more notes. * */ CovarianceAndContravarianceImpl <IExample1> covariance1 = new CovarianceAndContravarianceImpl <IExample1>(); IContraVariance <InterfaceExampleClass> contraVariance1 = covariance1; }