C# - 泛型委托

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 委托_泛型
 7 {
 8     class Program
 9     {
10         // 声明委托(无参,无返回值)
11         delegate void DG();
12 
13         static void Main(string[] args)
14         {
15             // 1.委托调用:SayHelloInChinese()
16             DG dg = SayHelloInChinese;
17             dg();
18          
19             // 2.委托调用:SayHelloInEnglish()
20             dg = SayHelloInEnglish;
21             dg();
22 
23             Console.WriteLine("\n===================\n");
24 
25             // 3. Action泛型委托: 无参, 无返回值  
26             Action dg1 = SayHelloInChinese;
27             dg1();
28 
29             // 4. Action泛型委托: 带参, 无返回值
30             Action<string>dg2 = SayHelloInEnglish;
31             dg2("张三");
32 
33             Console.WriteLine("\n===================\n");
34 
35             // 5. Func泛型委托 : 有参  有返回值   
36             Func<double, double, double> add = Add;
37             double result = add(3.0, 4.0);
38             Console.WriteLine("result = {0}", result);
39 
40 
41 
42             Console.ReadLine();
43         }
44 
45 
46         static void SayHelloInChinese()
47         {
48             Console.WriteLine("你好! ");
49         }
50 
51         static void SayHelloInChinese(string name)
52         {
53             Console.WriteLine("你好! {0}", name);
54         }
55 
56         static void SayHelloInEnglish()
57         {
58             Console.WriteLine("Hello! ");
59         }
60 
61         static void SayHelloInEnglish(string name)
62         {
63             Console.WriteLine("Hello! {0}", name);
64         }
65 
66         static double Add(double a, double b)
67         {
68             return a + b;
69         }
70 
71     }
72 }

posted @ 2016-03-31 16:54  C/C++/Python/Java  阅读(256)  评论(0编辑  收藏  举报