示例#1
0
        static void Main1(string[] args)
        {
            Mydele obj = new Mydele(Display);

            obj(5);

            Mydele1 obj1 = new Mydele1(add);
            int     i    = obj1(5);

            Console.WriteLine("add method called with delegate and addition is: " + i);
            Mydel2 obj2 = new Mydel2(multiply);
            int    c    = obj2(5, 6);

            Console.WriteLine("Multiply method called " + c);

            //Method in another class
            Class1 o    = new Class1();
            Mydel3 obj3 = Class1.divide;
            int    d    = obj3(25, 25);

            Console.WriteLine("Divide method called " + d);

            dele ob = o.dis;

            ob();
            Console.ReadLine();
        }
示例#2
0
        static void Main(string[] args)
        {
            string s   = "kk";
            Mydel  obj = delegate(int a, int b)
            {
                return(a + b);
            };

            Console.WriteLine(obj(10, 20));
            Mydel2 obj1 = delegate()
            {
                Console.WriteLine("Print the display function " + s);
            };

            obj1();

            Console.ReadLine();
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("***Quiz on Delegate***");
            int a = 25, b = 37, c = 100;
            A   obA1 = new A();
            A   obA2 = new A();

            Mydel1 del1 = obA1.Sum;//Pointing Sum(int a, int b)

            Console.WriteLine("del1 is pointing Sum(int a,int b):");
            Console.WriteLine("Sum of a and b is: {0}", del1(a, b));

            Mydel2 del2 = obA1.Sum;//pointing Sum(int a, int b, int c)

            Console.WriteLine("del2 is pointing Sum(int a,int b,int c):");
            Console.WriteLine("Sum of a, b and c is: {0}", del2(a, b, c));
            //same as
            //Console.WriteLine("Sum of a, b and c is: {0}", del2.Invoke(a, b, c));
            Console.ReadKey();
        }
示例#4
0
        static void Main(string[] args)
        {
            Vehicle vehicle1 = new Vehicle(); //ok
            Bus     bus1     = new Bus();     //ok

            Console.WriteLine("***Exploring Contravariance with C# delegates * **");
            //General case
            TakingDerivedTypeParameterDelegate del1 = bus1.ShowBus;

            del1(bus1);
            //Special case:
            //Contravariance:

            /*Note that the delegate expected a method that accepts a
             * bus(derived) object parameter but still it can point to the
             * method that accepts vehicle(base) object parameter*/

            TakingDerivedTypeParameterDelegate del2 = vehicle1.ShowVehicle; //重点!!!!

            //一个接受Bus作为参数的委托可以指向一个接受其父类(Vehicle)作为参数的方法

            del2(bus1); //但调用时需传入Bus对象,如果传入Vehicle对象会报错(因为signature不匹配)。

            //Additional note:you cannot pass vehicle object here
            //del2(vehicle1);//error

            Mydel2 myDel2 = vehicle1.ShowVehicle;
            Mydel2 myDel3 = bus1.ShowBus;   //报错

            //委托的参数是Vehicle,不能指向一个接受其子类Bus的方法!
            //(否则,你使用这个委托时,只知道这个委托接受Vehicle,可能会传入不符合被指向的方法要求的参数)
            //委托的返回类型如果是Vehicle的话,它可以指向一个返回类型是其子类Bus的方法(这里未演示)


            Console.ReadKey();
        }