码迷,mamicode.com
首页 > 其他好文 > 详细

最简单易懂的委托例子

时间:2014-11-13 18:34:40      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   os   sp   div   on   log   

 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         else
28             process = new ProcessDelegate(Divide);
29         Console.WriteLine("Result:{0}",process(param1 ,param2 ));
30     }
31 }

 

最简单易懂的委托例子

标签:style   blog   color   ar   os   sp   div   on   log   

原文地址:http://www.cnblogs.com/mvv118/p/4095513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!