Exemplo n.º 1
0
        /// <summary>
        /// 协变测试
        /// 1.协变类型参数,该参数只能用于输出的位置(例如方法的返回值)
        /// 2.仅泛型接口和泛型委托支持协变类型参数(泛型类不支持协变类型参数)
        ///    在接口和委托的类型参数上指定out修饰符可将其声明为协变参数
        /// 3.(由于历史原因)数组支持协变
        /// </summary>
        private void Test01()
        {
            MyStackA <Cat> catStack = new MyStackA <Cat>();
            //MyStackA<Animal> animalStack = catStack;//编译报错

            MyStackB <Cat>     catStackB    = new MyStackB <Cat>();
            IPoppable <Animal> animalStackB = catStackB;//利用协变
            Animal             a1           = animalStackB.Pop();

            //WashA(catStackB);
            //WashB(catStackB);
            WashC(catStackB);
        }
Exemplo n.º 2
0
        private static void GenericTypesTests()
        {
            Console.WriteLine("--- {0} ---", System.Reflection.MethodBase.GetCurrentMethod().Name);
            Stack <int> stack = new Stack <int>();

            stack.Push(5);
            stack.Push(10);
            int x = stack.Pop();
            int y = stack.Pop();
            int xx = 5, yy = 10;

            Generics.Swap(b: ref yy, a: ref xx);

            Type g1 = typeof(G <>);
            Type g2 = typeof(G <,>);

            Console.WriteLine(g2.GetGenericArguments().Count());
            Type g3 = typeof(G <int, int>);

            Console.WriteLine(g3.GetGenericArguments().Count());

            Console.WriteLine(++Bob <int> .Count);
            Console.WriteLine(++Bob <int> .Count);
            Console.WriteLine(++Bob <string> .Count);
            Console.WriteLine(++Bob <object> .Count);

            // Covariance
            {
                // Assuming that Bear subclasses Animal:
                var bears = new Stack <Bear>();
                bears.Push(new Bear());
                // Because bears implements IPoppable<Bear>, we can convert it to IPoppable<Animal>:
                IPoppable <Animal> animals = bears;  // Legal
                Animal             a       = animals.Pop();
            }
            // Contravariance
            {
                IPushable <Animal> animals = new Stack <Animal>();
                IPushable <Bear>   bears   = animals; // Legal
                bears.Push(new Bear());
            }
            Console.WriteLine("=====================================================================");
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            // Stack covariance
            Animal animal    = new Bear(); // Bear is convertible to Animal (asumption above)
            var    bearStack = new Stack <Bear>();

            bearStack.Push(animal as Bear);
            bearStack.Push(new Bear());
            IPoppable <Animal> animalPop = bearStack;

            animal = animalPop.Pop();
            //Stack<Animal> animalst = bears;  // not a covariant parameter, classes do not permit covariant paramters since data flow in both directions
            ZooCleaner.Wash(bearStack); // OK, covariant method

            // contravariance
            var animalStack             = new Stack <Animal>();
            IPushable <Bear>  bearPush  = animalStack;  // legal
            IPushable <Camel> camelPush = animalStack;  // legal, contravariant

            animalStack.Push(new Bear());
            animalStack.Push(new Camel());
            camelPush.Push(new Camel());
            bearPush.Push(new Bear());

            // Interface covariance
            string        s         = "Hello";
            object        o         = s;
            IFoo <string> fooString = new FooString();

            //IFoo<object> fooObject; fooObject = fooString; // HEY! interfaces should be covariant!!!! Whats up?

            // Array covariance
            Bear[]   bearArr   = new Bear[] { new Bear(), new Bear() };
            Animal[] animalArr = bearArr; // OK, covariant
            animalArr[1] = new Bear();
            //animalArr[1] = new Camel();   ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
            //animalArr[1] = new Animal();  ditto
        }
Exemplo n.º 4
0
 private void OnBubbleTap(IPoppable bubble)
 {
     bubble.Pop();
     OnBubblePoped?.Invoke(bubble.GetScore());
 }