コード例 #1
0
        public static void CovariantGeneric()
        {
            ICovariantGeneric<MiddleClass> genericMiddle = new CovariantGeneric<MiddleClass>();

            // This is OK:
            ICovariantGeneric<BaseClass> genericBase = genericMiddle;

            // 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;
        }
コード例 #2
0
        public static void CovariantGeneric()
        {
            ICovariantGeneric <MiddleClass> genericMiddle = new CovariantGeneric <MiddleClass>();
            MiddleClass result = genericMiddle.Method();

            // 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;
        }