Example #1
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();
        }