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
using System;2

3
namespace ConsoleApplication14
{5
class Program6
{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

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

