日期:2014-05-17 浏览次数:20802 次
namespace Delegate {
//定义委托,它定义了可以代表的方法的类型
public delegate void GreetingDelegate(string name);
//新建的GreetingManager类
public class GreetingManager
{
//这里加了个event
public event GreetingDelegate MakeGreet;
public void GreetPeople(string name, GreetingDelegate MakeGreeting)
{
MakeGreeting(name);
}
}
class Program
{
private static void EnglishGreeting(string name)
{
Console.WriteLine("Morning, " + name);
}
private static void ChineseGreeting(string name)
{
Console.WriteLine("早上好, " + name);
}
static void Main(string[] args)
{
GreetingManager gm = new GreetingManager();
//声明了event后,就不能直接gm.MakeGreet=XXX来赋值了..
gm.MakeGreet += ChineseGreeting;