【Notes】委托的示例

 1 class Program
 2     {
 3         delegate double ProcessDelegate(double param1, double param2);//定义一个double型的委托
 4         static double Multiply(double param1, double param2)
 5         {
 6             return param1 * param2;
 7         }
 8         static double Divide(double param1, double param2)
 9         {
10             return param1 / param2;
11         }
12 
13         static void Main(string[] args)
14         {
15             
16             Console.WriteLine("Please enter two number:");
17             string input = Console.ReadLine();
18             int commaPos = input.IndexOf(',');//获取索引
19             double param1 = Convert.ToDouble(input.Substring(0, commaPos));
20             double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - 1-commaPos));
21             //询问是否相乘还是想除?
22             Console.WriteLine("Please enter M to Multiply or D to Divide:");
23             string input1=Console.ReadLine();
24             if (input1 == "M")
25             {
26                 process = new ProcessDelegate(Multiply);//初始化委托变量
27             }
28             else
29             {
30                 process = new ProcessDelegate(Divide);
31             }
32             Console.WriteLine("Result:{0}", process(param1,param2));        
33             Console.ReadKey();
34             
35         }
36     }
posted on 2009-09-11 19:10  c_sharp  阅读(231)  评论(0)    收藏  举报