Beispiel #1
0
        public static void RunDemo()
        {
            //Anonymous functions in C#
            SimplePrintDel spd = delegate()
            {
                Console.WriteLine("Simple print without param");
            };

            spd.Invoke();

            //Lamda expression in C#
            SimplePrintDel nd = () =>
            {
                Console.WriteLine("Simple print without param Lamda");
            };

            nd.Invoke();

            //=================With C# provided delegate methods=====
            action = delegate()
            {
                Console.WriteLine("Using Action Delegate");
            };
            action.Invoke();

            action = () =>
            {
                Console.WriteLine("Using Action Lamda");
            };
            action.Invoke();
            //=================================
            var msg = "Message with anonymous method and action param";

            actionWithParam = (x) => {
                Console.WriteLine(x);
            };
            actionWithParam.Invoke(msg);

            SimpleRetunDel rd = delegate() { return(39); };

            Console.WriteLine("Value : " + rd.Invoke());

            rd = () => { return(290); };
            Console.WriteLine("Value : " + rd.Invoke());

            SimpleRetunDel2 srd = delegate(int a, int b)
            {
                return(a * b);
            };

            Console.WriteLine("Value : " + srd.Invoke(10, 30));

            srd = (a, b) => a * b;
            Console.WriteLine("Value : " + srd.Invoke(10, 30));

            Console.ReadKey();
        }
Beispiel #2
0
        public static void RunDemo()
        {
            //Executing a Print function using delegate
            SimplePrintDel spd = Print;

            spd.Invoke();

            //Executing a parameteric function using delegate
            PrintDelegate pd  = Print;
            string        msg = "This is another way of calling parametric delegate";

            pd.Invoke(msg);

            //Executing a complex parametric function using delegate
            PrintGrades pgd = ShowGrades;

            pgd.Invoke(StudentGrades.Intermdiate);
            pgd.Invoke(StudentGrades.Advanced);
            pgd.Invoke(StudentGrades.Standard);

            //Multicasting Delegates
            pd += Print2;
            pd += Print3;

            pd.Invoke("Multi-Casting....");

            //C# delegates
            ActionDel  = Print;
            ActionDel2 = Print;
            ActionDel();
            ActionDel2("Action Del 2");

            //Retrun value delegate
            SumDel sum = Sum;
            var    rv  = sum.Invoke(10, 400);

            Console.WriteLine("Value : " + rv);

            FuncDel = Sum;
            rv      = FuncDel.Invoke(103, 500);
            Console.WriteLine("Value : " + rv);

            Console.ReadKey();
        }