Ejemplo n.º 1
0
        public static void CovariantGeneric()
        {
            ICovariantGeneric <MiddleClass> genericMiddle = new CovariantGeneric <MiddleClass>();
            MiddleClass result = genericMiddle.Method();

            ICovariantGeneric <BaseClass> genericMiddle1 = new CovariantGeneric <MiddleClass>();
            ICovariantGeneric <BaseClass> genericMiddle2 = new CovariantGeneric <LastClass>();
            var a = genericMiddle1.Method();
            var b = genericMiddle2.Method();


            ICovariantGeneric <MiddleClass> genericMiddle3 = new CovariantGeneric <LastClass>();

            ICovariantGeneric <LastClass> genericMiddle4 = new CovariantGeneric <LastClass>();


            // This is OK here:
            ICovariantGeneric <BaseClass> genericBase = genericMiddle;
            BaseClass baseResult = genericBase.Method();

            // This will produce compile-time error:
            // Cannot implicitly convert type 'ICovariantGeneric<MiddleClass>' to 'ICovariantGeneric<LastClass>'.
            // An explicit conversion exists (are you missing a cast?)
            //// ICovariantGeneric<LastClass> genericLast = genericMiddle;
        }
Ejemplo n.º 2
0
 private static void WithCovariantGeneric()
 {
     ICovariantGeneric <MIddleClass> covariantGeneric = new CovariantGeneric <LastClass>();
     var result = covariantGeneric.Method();
 }