C# 委托与事件

具体流程如下:

1.类外声明委托
public delegate void SaySomething(string name);

2.写方法

public void SayHello(string name) { Console.WriteLine(“Hello,” + name + “!”); }
public void SayNiceToMeetYou(string name) { Console.WriteLine(“Nice to meet you,” + name + “!”); }

3.声明事件
public event SaySomething come;
//SaySomething是委托名,come是事件名。

4.实例化委托,并将方法赋给事件(用+=)
public void test()
{
SaySomething sayhello = new SaySomething(SayHello);
SaySomething saynice = new SaySomething(SayNiceToMeetYou); come += sayhello;
come += saynice;
come(“张三”); //用事件去调用所有委托的方法
}
C# 委托与事件

上一篇:7-6 区间覆盖 (10 分)(思路+详解)Come 宝!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


下一篇:面向轨交信息化,领存FT2000-4 COME核心板出击!