委托事件
1.解耦
using System.Collections.Generic; namespace MyDelegate { public class ListExtend { #region 数据准备 /// <summary> /// 准备数据 /// </summary> /// <returns></returns> public List<Student> GetStudentList() { #region 初始化数据 List<Student> studentList = new List<Student>() { new Student() { Id=1, Name="老K", ClassId=2, Age=35 }, new Student() { Id=1, Name="hao", ClassId=2, Age=23 }, new Student() { Id=1, Name="大水", ClassId=2, Age=27 }, new Student() { Id=1, Name="半醉人间", ClassId=2, Age=26 }, new Student() { Id=1, Name="风尘浪子", ClassId=2, Age=25 }, new Student() { Id=1, Name="一大锅鱼", ClassId=2, Age=24 }, new Student() { Id=1, Name="小白", ClassId=2, Age=21 }, new Student() { Id=1, Name="yoyo", ClassId=2, Age=22 }, new Student() { Id=1, Name="冰亮", ClassId=2, Age=34 }, new Student() { Id=1, Name="瀚", ClassId=2, Age=30 }, new Student() { Id=1, Name="毕帆", ClassId=2, Age=30 }, new Student() { Id=1, Name="一点半", ClassId=2, Age=30 }, new Student() { Id=1, Name="小石头", ClassId=2, Age=28 }, new Student() { Id=1, Name="大海", ClassId=2, Age=30 }, new Student() { Id=3, Name="yoyo", ClassId=3, Age=30 }, new Student() { Id=4, Name="unknown", ClassId=4, Age=30 } }; #endregion return studentList; } #endregion #region 普通检索 public List<Student> SelectStuAge() { List<Student> stuList = this.GetStudentList(); List<Student> list = new List<Student>(); foreach (var item in stuList) { if (item.Age > 25) { list.Add(item); } } return list; } public List<Student> SelectStuName() { List<Student> stuList = this.GetStudentList(); List<Student> list = new List<Student>(); foreach (var item in stuList) { if (item.Name.Length > 2) { list.Add(item); } } return list; } public List<Student> SelectStuClassId() { List<Student> stuList = this.GetStudentList(); List<Student> list = new List<Student>(); foreach (var item in stuList) { if (item.ClassId == 2) { list.Add(item); } } return list; } #endregion #region 委托检索 public delegate bool SelectStuDelegate(Student student); public bool SelectStuByAge(Student student) { if (student.Age > 25) { return true; } return false; } public bool SelectStuByName(Student student) { if (student.Name.Length > 2) { return true; } return false; } public bool SelectStuByClassId(Student student) { if (student.ClassId == 2) { return true; } return false; } /// <summary> /// 委托解耦,减少重复代码 /// </summary> /// <param name="stuList"></param> /// <param name="method"></param> /// <returns></returns> public IEnumerable<Student> SelectStu(IEnumerable<Student> stuList, SelectStuDelegate method) { //return stuList.Where(n => method(n)); List<Student> list = new List<Student>(); foreach (var item in stuList) { if (method(item)) { list.Add(item); } } return list; } #endregion } public class Student { public int Id { get; set; } public string Name { get; set; } public int ClassId { get; set; } public int Age { get; set; } } }
2.异步多线程
{ Console.WriteLine("------------WithReturnNoPara---------------"); WithReturnNoPara withReturnNoPara1 = new WithReturnNoPara(new Program().GetNum1); WithReturnNoPara withReturnNoPara2 = new WithReturnNoPara(new Program().GetNum2); WithReturnNoPara withReturnNoPara3 = new WithReturnNoPara(new Program().GetNum3); Console.WriteLine(withReturnNoPara1.BeginInvoke(null, null));//异步多线程调用 Console.WriteLine(withReturnNoPara1()); Console.WriteLine(withReturnNoPara2()); Console.WriteLine(withReturnNoPara3()); }
3.多播委托,
限制:
- 多播不能使用异步多线程
- 返回值多播委托,只返回最后一个值
有什么用:
- 一个变量保存多个值(方法),可以增减,Invoke 的时候可以按顺序执行
{ //按顺序添加方法,形成方法链, Console.WriteLine("------------WithReturnNoPara多播---------------"); Program program = new Program(); WithReturnNoPara method = new WithReturnNoPara(new Program().GetNum1); method += new WithReturnNoPara(program.GetNum2); method += new WithReturnNoPara(program.GetNum3); method += new WithReturnNoPara(GetNum4); method -= new WithReturnNoPara(GetNum4);//遇到一模一样的 才会去掉 method -= new WithReturnNoPara(program.GetNum3); //method.BeginInvoke(null, null);//多播,不能使用 BeginInvoke foreach (WithReturnNoPara item in method.GetInvocationList()) { Console.WriteLine(item.Invoke()); } }