1
using System;
2
3
namespace ConsoleApplication1
4
{
5
class Program
6
{
7
delegate string delegateTest(string val);
8
9
//被委托调用的方法
10
static string Test(string str)
11
{
12
str += " 这是传统1.0的方法";
13
return str;
14
}
15
16
static void Main(string[] args)
17
{
18
//匿名方法写法很简捷
19
delegateTest anonDel = delegate(string str)
20
{
21
str += " 这是C#2.0的匿名方法";
22
return str;
23
}; //注意"}"后有一个分号
24
25
Console.WriteLine(anonDel("Hello World!"));
26
27
//传统的委托使用方法
28
delegateTest DT = new delegateTest(Test);
29
Console.WriteLine(DT("Hello C#!"));
30
Console.ReadKey();
31
}
32
}
33
}
34
35

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

运行结果:
Hello World! 这是C#2.0的匿名方法
Hello C#! 这是传统1.0的方法