Delegate: Store function Reference type
return type and parameter consistency
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace Ch06ex05{class Program {delegate double processdelegate (double param1, double param2); Static double Multiply (double param1, double param2) {return param1 * PARAM2; } static double Divide (double param1, double param2) {return param1/param2; } static void Main (string[] args) {processdelegate process; Console.WriteLine ("Enter 2 numbers separated with a comma:"); string input = Console.ReadLine (); int commapos = input. IndexOf (', '); Double param1 = convert.todouble (input. Substring (0, Commapos)); Double param2 = convert.todouble (input. Substring (Commapos + 1, input. LENGTH-COMMAPOS-1)); Console.WriteLine ("Enter M to multiply or D to divide:");input = Console.ReadLine (); if (input = = "M") process = new Processdelegate (Multiply); else process = new Processdelegate (Divide); Console.WriteLine ("Result: {0}", Process (param1, param2)); Console.readkey (); } }}
Why not judge the parameters to pass in and choose different function methods, rather than to delegate it also want to new delegate class ...
C # Delegate Delegate