public static int ReturnDelegateResult(NumericDelegate numericDelegate, int a, int b) { if (numericDelegate != null) { return(numericDelegate(a, b)); } return(0); }
public static void Main(string[] args) { NumericDelegate numericDelegateAdd = new NumericDelegate(DelegateImplementor.Add); NumericDelegate numericDelegateSubract = new NumericDelegate(DelegateImplementor.Subract); Console.Write("Enter the first number:"); int a = int.Parse(Console.ReadLine()); Console.Write("Enter the second number:"); int b = int.Parse(Console.ReadLine()); Console.WriteLine("Addition: {0}", numericDelegateAdd(a, b)); Console.WriteLine("Subraction: {0}", numericDelegateSubract(a, b)); Console.WriteLine("Delegate_Test Result 1 :{0}", Delegate_Test.ReturnDelegateResult(numericDelegateAdd, a, b)); Console.WriteLine("Delegate_Test Result 2 :{0}", Delegate_Test.ReturnDelegateResult(numericDelegateSubract, a, b)); Console.WriteLine("Delegate_Test Result 3 :{0}", Delegate_Test.ReturnDelegateResult(null, a, b)); NumericDelegate nc = numericDelegateAdd + numericDelegateSubract; Console.WriteLine("Multi Cast: {0}", nc(a, b)); Console.ReadLine(); }