.net基础—委托和事件
委托
委托是一种引用类型,表示对具有特定参数列表和返回类型的方法的引用。 在实例化委托时,可以将其实例与任何具有兼容签名和返回类型的方法相关联。 可以通过委托实例调用方法。
可以将任何可访问类或结构中与委托类型匹配的任何方法分配给委托。 该方法可以是静态方法,也可以是实例方法。 此灵活性意味着可以通过编程方式来更改方法调用,还可以向现有类中插入新代码。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | using System; namespace Test03 { internal class Program { static void Main( string [] args) { MyDelegate myDelegate = new MyDelegate(); myDelegate.Show(); Console.ReadKey(); } } public class MyDelegate { //声明委托 //public delegate void NoReturnNoPara<T>(T t); //public delegate void NoReturnWithPara(int x, int y); public delegate void NoReturnNoPara(); public void Show() { { //委托的实例化 (传递的方法要与定义委托的返回值和参数相同) NoReturnNoPara method = new NoReturnNoPara( this .DoNothing); //委托实例的调用 method.Invoke(); //调用 方式二 //method(); } } private void DoNothing() { Console.WriteLine( "This is DoNothing" ); } private static void DoNothingStatic() { Console.WriteLine( "This is DoNothingStatic" ); } } } |
委托应用实例
假如现在有一个数据集,要对其中的数据按照相应的掉进进行筛选。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | namespace Test03 { internal class Program { static void Main( string [] args) { var dataList = GetStudentList(); { //查询年龄大于20 List<Student> result = new List<Student>(); foreach ( var item in dataList) { if (item.Age > 20) { result.Add(item); } } Console.WriteLine($ "年龄大于20的共有{result.Count}人" ); } { //查询二班中年龄大于18的 List<Student> result = new List<Student>(); foreach ( var item in dataList) { if (item.ClassId == 2 && item.Age > 18) { result.Add(item); } } Console.WriteLine($ "二班中年龄大于18的共有{result.Count}人" ); } //查询姓名长度大于2的 /* * 具体代码和上面的类似 */ Console.ReadKey(); } /// <summary> /// //初始化数据 /// </summary> /// <returns></returns> public static List<Student> GetStudentList() { List<Student> studentList = new List<Student>() { new Student() { Id = 1, Name = "萧峰" , ClassId = 1, Age =23 }, new Student() { Id = 2, Name = "段誉" , ClassId = 1, Age =22 }, new Student() { Id = 3, Name = "虚竹" , ClassId = 1, Age =21 }, new Student() { Id = 4, Name = "王语嫣" , ClassId = 2, Age =21 }, new Student() { Id = 5, Name = "阿紫" , ClassId = 2, Age =18 }, new Student() { Id = 6, Name = "阿朱" , ClassId = 2, Age =22 } }; return studentList; } } } |
对于上面代码,可以使用委托将上面的代码进行优化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | using System; using System.Collections.Generic; namespace Test03 { //声明委托 public delegate bool ThanDelegate(Student student); class Program { static void Main( string [] args) { //调用 DemoDelegate demo = new DemoDelegate(); demo.Show1(); Console.ReadKey(); } } #region 委托示例 public class DemoDelegate { public void Show1() { var dataList = StudentList.GetStudentList(); ThanDelegate method = new ThanDelegate(Than); var result = this .GetListDelegate(dataList, method); Console.WriteLine($ "年龄大于20的共有{result.Count}人" ); } /// <summary> /// 通的筛选方法 /// </summary> /// <param name="source">数据源</param> /// <param name="method">委托</param> /// <returns></returns> private List<Student> GetListDelegate(List<Student> source, ThanDelegate method) { List<Student> result = new List<Student>(); foreach (Student student in source) { if (method.Invoke(student)) { result.Add(student); } } return result; } /// <summary> /// 查询年龄大于20 /// </summary> private bool Than(Student student) { return student.Age > 20; } } #endregion #region 数据 class StudentList { /// <summary> /// //初始化数据 /// </summary> /// <returns></returns> public static List<Student> GetStudentList() { List<Student> studentList = new List<Student>() { new Student() { Id = 1, Name = "萧峰" , ClassId = 1, Age =23 }, new Student() { Id = 2, Name = "段誉" , ClassId = 1, Age =22 }, new Student() { Id = 3, Name = "虚竹" , ClassId = 1, Age =21 }, new Student() { Id = 4, Name = "王语嫣" , ClassId = 2, Age =21 }, new Student() { Id = 5, Name = "阿紫" , ClassId = 2, Age =18 }, new Student() { Id = 6, Name = "阿朱" , ClassId = 2, Age =22 } }; return studentList; } } #endregion } |
Student:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class Student { public int Id { get ; set ; } public string Name { get ; set ; } public int ClassId { get ; set ; } public int Age { get ; set ; } public void Study() { Console.WriteLine( "This is Study" ); } public static void StudyStatic() { Console.WriteLine( "This is StudyStatic" ); } public static void Show() { Console.WriteLine( "Hello" ); } } |
事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | namespace TestEvent { public delegate void MyDelegate( string name); class Program { static void Main( string [] args) { EventSource test = new EventSource(); test.TestEvent(); Console.ReadKey(); } } public class EventSource { public event MyDelegate EventDelegate; public void SetCustomer( string name) { Console.WriteLine($ "Hello,{name}" ); } public void TestEvent() { EventSource source = new EventSource(); source.EventDelegate += new MyDelegate(source.SetCustomer); source.EventDelegate( "TanYongJun" ); } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器