Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            RegularDelegate regDelegate = lineMethod; //creating new instance of RegularDelagate and assigning lineMethod to it
            Line            myLine      = regDelegate(10);

            myLine.Display();
            Console.WriteLine("--------------------------");
            regDelegate = squareMethod; //assigning squareMethod (which returns Square object) to regDelegate
                                        //regDelegate is of type RegularDelegate, its signature indicates that its return type is Line
                                        //this concept is covariance
                                        //covariance is a rule which allows to assing also a method which return type is
                                        //derived from return type in signature
            Square mySquare = (Square)regDelegate(7);

            mySquare.Display();
            Console.WriteLine("--------------------------");
            ContraDelegate cntDelegate = intMethod; //creating new instance of ContraDelegate and assigning intMethod to it
                                                    //cntDelegate is of type ContraDelegate, its signature indicates that
                                                    //its argument has to be of type Square. no contravariance here
            int result = intMethod(mySquare);

            Console.WriteLine("[int]result: {0}", result);
            Console.WriteLine("--------------------------");
            cntDelegate = intMethodCtr; //assiging intMethodCtr to instance of COntraDelegate. Its signature indicates that
                                        //its argument has to be of type Square, however intMethodCtr's argument is of type Line.
                                        //this concept is contravariance
            int result2 = intMethodCtr(myLine);

            Console.WriteLine("[int]result2: {0}", result2);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // covariance in generic delegates = you can assign delegate with mode derived param type to less derived
            //CoDelegate<Base> some_co_del_inst = SomeCoMethod1;
            //CoDelegate<Derived> some_co_del_inst1 = SomeCoMethod1;
            //some_co_del_inst = some_co_del_inst1;
            //some_co_del_inst();
            //some_co_del_inst1();

            // contravarianca in generic delegates = you can assign delegate with less derived param type to more derived
            ContraDelegate <Base>    some_contra_del_inst  = SomeContraMethod1;
            ContraDelegate <Derived> some_contra_del_inst1 = SomeContraMethod1;

            some_contra_del_inst();
            some_contra_del_inst1();

            some_contra_del_inst1 = some_contra_del_inst;

            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("*** Testing Contra-variance with Generic Delegates.***");
            Vehicle obVehicle = new Vehicle();
            Bus     obBus     = new Bus();

            Console.WriteLine("Normal usage:");
            ContraDelegate <Vehicle> contraVehicle = ShowVehicleType;

            contraVehicle(obVehicle);
            ContraDelegate <Bus> contraBus = ShowBusType;

            contraBus(obBus);
            Console.WriteLine("Using contravariance now.");

            /*
             * Using general type to derived type.
             * Following assignment is Ok, if you use 'in' in delegate definition.
             * Otherwise, you'll receive compile-time error.
             */
            contraBus = contraVehicle;//ok
            contraBus(obBus);
            Console.ReadKey();
        }