示例
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
delegate void Printer(string s);
class Program
{
static void Main(string[] args)
{
// 使委托与匿名方法关联
Printer p = delegate (string j)
{
System.Console.WriteLine(j);
};
p("The delegate using the anonymous method is called.");
// 使委托与命名方法 (DoWork) 关联
p = new Printer(DoWork);
p("The delegate using the named method is called.");
Console.ReadLine();
}
static void DoWork(string k)
{
System.Console.WriteLine(k);
}
}
}
运行测试