第16节、C#委托与事件
1、C#委托理解
看标题便疑问:
- 委托是什么?
- 为什么需要委托?
- 委托能用来做什么?
- 如何定义委托?怎么使用?
参考回答
1、顾名思义,让别人协助帮忙你处理某件事情,其实际意义便是让别人代理你的事情。
2、使用关键字delegate关键字声明委托
委托(Delegate)特别用于实现事件和回调方法,所有的委托都派生自System.Delegate类。
2、C#委托定义
委托声明决定了可由该委托引用的方法,委托可指向一个与具有相同标签的方法。
修饰符 delegate 返回类型 委托名(形参);
- 委托声明与接口方法差不多,只是返回类型前多一个delegate关键字,修饰符一般是public类型,因为供别人调用。
- 委托本质也是一个类型。我们声明一个类可以实例化、同时委托也可以实现实例。
示例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 控制台应用 { public delegate void MyDelegatehl(string name);//声明委托 public class 委托 { public void GetPrint(string name) { Console.WriteLine("问候:你好:{0}", name); } } /// <summary> /// 控制台输出 /// </summary> public class TestProgramA { public static void Main(string[] args) { 委托 wtl = new 委托(); MyDelegatehl dl = wtl.GetPrint; dl("小明"); Console.ReadKey(); } } }
输出:问题:你好:小明
2、多播委托
包含多个方法的委托叫做多播委托。using System;
using System.Collections.Generic; using System.Linq; using System.Text; namespace 控制台应用 { public delegate void MyDelegate(string name);//声明委托 public class 多播委托 { public void GetPrint(string name) { Console.WriteLine("问候:你好:{0}",name); } public void GetPrint2(string name) { Console.WriteLine("问候:早上好:{0}", name); } //委托相对参数类型 public void GetPrints(MyDelegate mydele, string name) { mydele(name); } } /// <summary> /// 控制台输出 /// </summary> public class TestProgram { public static void Main(string[] args) { 多播委托 wtl = new 多播委托(); //方式一: MyDelegate dl1, dl2; //理解委托就是数据类型,给数据赋值(把方法理解为参数值) dl1 = wtl.GetPrint; dl2 = wtl.GetPrint2; dl1("张三"); dl2("李四"); Console.WriteLine(); //方式二 MyDelegate dl3 = wtl.GetPrint; //委托可以是要+=/-=赋值与移除,如果参数值一样的两个方法就可以是要+=,-= dl3 += wtl.GetPrint2; dl3("王五"); Console.WriteLine(); //方式三 //理解方式一,方法参数返回值与委托一致,都可以存到同一个委托上,理解委托是个类型,就可以什么一个方法GetPrints(委托类型,值)的方法 MyDelegate dl4 = wtl.GetPrint; wtl.GetPrints(dl4, "张三"); MyDelegate dl5 = wtl.GetPrint2; wtl.GetPrints(dl5, "李四"); Console.WriteLine(); //方式四 MyDelegate dl6 = wtl.GetPrint; //委托可以是要+=/-=赋值与移除,如果参数值一样的两个方法就可以是要+=,-= dl6 += wtl.GetPrint2; wtl.GetPrints(dl6, "王五"); Console.ReadKey(); } } }
输出:
问候:你好:张三
问候:早上好:李四
问候:你好:王五
问候:早上好:王五
问候:你好:张三
问候:早上好:李四
问候:你好:王五
问候:早上好:王五
3、事件声明
示范:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 控制台应用 { public delegate void MyDelegate1(); //声明委托无参 public delegate void MyDelegate2(string name); //声明委托有参 public class 委托_事件 { public event MyDelegate1 MyEvent; //声明事件无参 public event MyDelegate2 MyEvent2; //声明事件有参 public void GetPr() { if (MyEvent != null) //判断事件非空 { MyEvent(); //执行事件,即是执行添加事件中委托的方法 } } public void GetPr2(string name) { if (MyEvent2 != null) //判断事件非空 { MyEvent2(name); //执行事件,即是执行添加事件中委托的方法 } } } }
4、委托与事件
示范:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 控制台应用 { public class 委托和事件 { public static void Main(string[] args) { 委托_事件 wt1 = new 委托_事件(); //实例对象 wt1.MyEvent += GetPrint; //给对象事件家方法 wt1.MyEvent += GetPrint2; wt1.GetPr(); //执行 Console.WriteLine(); wt1.MyEvent2 += GetPrint; //给对象事件家方法 wt1.MyEvent2 += GetPrint2; wt1.GetPr2("王五"); //执行 Console.ReadKey(); } public static void GetPrint() { Console.WriteLine("问候:你好:张三"); } public static void GetPrint2() { Console.WriteLine("问候:早上好:李四"); } public static void GetPrint(string name) { Console.WriteLine("问候:你好:{0}", name); } public static void GetPrint2(string name) { Console.WriteLine("问候:早上好:{0}",name); } } }
输出
问候:你好:张三
问候:早上好:李四
问候:你好:王五
问候:早上好:王五