首页 > 代码库 > 最简单易懂的委托例子

最简单易懂的委托例子

 1 class Program 2 { 3     delegate double ProcessDelegate(double param1, double param2); 4       5     static double Multiply(double param1, double param2) 6     { 7         return param1*param2; 8     } 9  10     static double Divide(double param1, double param2)11     {12         return param1/param2;13     }14  15     static void Main(string[] args)16     {17         ProcessDelegate process;18         Console.WriteLine("Enter 2 number separated with a comma:");19         string input = Console.ReadLine();20         int commaPos = input.IndexOf(,);21         double param1 = Convert.ToDouble(input.Substring(0,commaPos));22         double param2 = Convert.ToDouble(input.Substring(commaPas+1,input.Length - commaPos-1));23         Console.WriteLine("Enter M or D:");24         input = Console>readLine();25         if(input == "M")26             process = new ProcessDelegate(Multiply);27         else28             process = new ProcessDelegate(Divide);29         Console.WriteLine("Result:{0}",process(param1 ,param2 ));30     }31 }

 

最简单易懂的委托例子