using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test1
{
public delegate void SayDlg(string str);
class Program
{
// 事件 被限制的委托变量
public static event SayDlg SayEvent;
static void Main(string[] args)
{
// 委托声明
// 方法以变量的形式传递,并以方法的形式执行
SayDlg dlg = SayHello;
// 委托链
dlg += SayWorld;
dlg += SayWorld;
dlg -= SayWorld;
dlg("123");
// 匿名函数 只使用一次
SayDlg dlg1 = delegate (string str)
{
Console.WriteLine($"{str},匿名");
};
dlg1("345");
SayDlg dlg2 = (str) => { Console.WriteLine($"{str},lambda"); };
dlg2("678");
// 注册事件
SayEvent += Program_SayEvent;
//SayEvent -= Program_SayEvent;
if(SayEvent != null)
{
// 触发事件
SayEvent("91011");
}
Console.ReadKey();
}
private static void Program_SayEvent(string str)
{
Console.WriteLine($"{str},event");
}
public static void SayHello(string str)
{
Console.WriteLine($"{str},hello");
}
public static void SayWorld(string str)
{
Console.WriteLine($"{str}, World");
}
}
}