Example #1
0
        static void Main(string[] args)
        {
            // about Func<> and Action<>
            Sum(10, 20);
            Console.WriteLine($"Diff of values : {Diff(20, 10)}");
            int diff = Diff(20, 10);

            Console.WriteLine($"MPY of values : {MPY(20, 10)}");
            Console.WriteLine($"DIV of values : {DIV(20, 10)}");
            Console.WriteLine("\n Calling using Delegate");
            DelegateSum delSum = Sum; // this assigns the reference of Sum to delSum

            Func <int, int, int> delDiff2 = Diff;
            //Func<int,int, string> FuncDel = SomeMethod1;
            //Func<int, string> FuncDel2 = SomeMethod2;
            //Func<double, double, string> FuncDel3 = SomeMethod3;
            //The last First and rest of state ments except last one is conditional statements and the last one is return value.

            //Action <t>delegate is to refer void method
            //Action(<int, int, int, string> funDel = SomeMethod4;
            //Action(<int, int, string> funDel = SomeMethod5;
            //Action(<double, double, string> funDel = SomeMethod6;


            DelegateDiff delDiff = Diff;

            Console.WriteLine($"Using delegate Diff of values: {delDiff(20, 10)}");
            DelegateMPY delMpy = MPY;

            Console.WriteLine($"Using delegate MPY of values : {MPY(20, 10)}");
            //Console.WriteLine(delSum);
            //Call using delegate
            delSum(100, 200); // this is method call to SUm using delegate
            delSum(50, 30);   // this is method call to SUm using delegate
        }//end main
        public static void Main()
        {
            DelegateSum delegateSum = (a, b) =>
            {
                return(a + b);
            };
            DelegateMult delegateMult = (a, b) =>
            {
                return(a * b);
            };
            double sum  = delegateSum(8.76, 10.86);
            double mult = delegateMult(8.76, 10.86);

            Console.WriteLine("Sum is: " + sum);
            Console.WriteLine("Multiplication is: " + mult);
            Console.ReadLine();
        }
Example #3
0
        public static void Main()
        {
            DelegateClass obj = new DelegateClass();
            //Step2: Instantiating the delegate, means create object of delegate and pass the method name which it will reference to.
            //we can pass any method which is matching the signature of delegate.
            DelegateSum  delegateSum  = new DelegateSum(obj.AddNum);
            DelegateMult delegateMult = new DelegateMult(obj.MultNum);
            //Step-3: Now call the delegate by passing required parameter values, so that internally the method which is bound with the delegate gets executed.
            double sum  = delegateSum(8.76, 10.86);
            double mult = delegateMult(8.76, 10.86);

            // we can call the delegate by using Invoke method===
            sum  = delegateSum.Invoke(8.76, 10.86);
            mult = delegateMult.Invoke(8.76, 10.86);
            //=============================================
            Console.WriteLine("Sum is: " + sum);
            Console.WriteLine("Multiplication is: " + mult);
            Console.ReadLine();
        }