C#委托和事件
class Program { #region 委托作用 解耦,减少重复代码、异步多线程 #endregion #region Delegate委托 有返回值,无返回值都行 ,有参数无参数都行 //public delegate void myDelegate(); //static void Main(string[] args) //{ // myDelegate d = new myDelegate(method1); // print(d); //} //public static void print(myDelegate d) //{ // d(); //} //public static void method1() //{ // Console.WriteLine("Hello World"); // Console.ReadKey(); //} #endregion #region Action委托 无返回值 有无参数都行 //static void Main(string[] args) //{ // Action<int> a = printInt; //泛型代表方法有参数无返回值 没有泛型方法无参数无返回值 // a(3); // Action b = printInt; // b(); //} //public static void printInt() //{ // Console.WriteLine("Hello World!"); // Console.ReadKey(); //} //public static void printInt(int a) //{ // Console.WriteLine(a); // Console.ReadKey(); //} #endregion #region Func委托 有返回值 有无参数都行 //static void Main(string[] args) //{ // Func<int, string> a = printInt; //Func 有返回值,最后一个泛型是返回值类型 // Console.WriteLine(a(3)); // Console.ReadKey(); //} //public static string printInt(int a) //{ // return "1" + a; //} #endregion #region 多播委托 一般是无返回值的 有无参数都行 //static void Main(string[] args) //{ //Action<int> a = test1; //a += test2; //a -= test2; //a(2); //lambda表达式注册之后用多播委托去不掉 //Action<int> a = (b) => { Console.WriteLine("test1" + b); }; //a += (b) => { Console.WriteLine("test2" + b); }; //a -= (b) => { Console.WriteLine("test2" + b); }; //a(3); //Action a = show; //a += new Person().study; //a -= new Person().study;//两个方法不是同一个对象调用的====>去不掉==》调用两次 //a(); #region 方式二 //Delegate[] list = a.GetInvocationList(); //foreach (Delegate d in list) //{ // d.DynamicInvoke(0); //} #endregion //Console.ReadKey(); //} static void test1(int a) { Console.WriteLine("test1" + a); } static void test2(int b) { Console.WriteLine("test2" + b); } static void show() { Console.WriteLine("show"); } #endregion #region 匿名方法 public delegate void ElapsedEventHandler(int sender, int e); static void Main(string[] args) { #region 正常方法 //Func<int, int, int> func = test; //Console.WriteLine(func(2, 2)); //Console.ReadKey(); #endregion #region 匿名方法 有无参数,有无返回值都行(当参数没用到的时候参数可以省略) //Func<int, int, int> func = delegate(int a,int b) // { // return 1 + 2; // }; //Console.WriteLine(func(2, 3)); //Console.ReadKey(); #endregion #region 匿名方法 (当参数没用到的时候参数可以省略) //Func<int, int, int> func = delegate //{ // return 1 + 2; //}; //Console.WriteLine(func(2, 3)); //Console.ReadKey(); #endregion #region 匿名方法 无参数()可以省略 //Action func = delegate //{ // Console.WriteLine("234"); //}; //func(); //Console.ReadKey(); #endregion #region lambda表达式 //Func<int, int, int> func = (arg1, arg2) => // { // return arg2 + arg1; // }; //Console.WriteLine(func(3, 2)); //Console.ReadKey(); #endregion } static int test(int arg1, int arg2) { return arg1 + arg2 + 3; } static void test2(int arg1, int arg2) { Console.WriteLine(arg1 + arg2); Console.ReadKey(); } #endregion } public class Person { public void study() { Console.WriteLine("study!!!!!!!!"); } public void Method() { Console.WriteLine("Method!!!!!!!!"); } }
拓展:调用委托实例通用写法:
1.委托实例.Invoke(参数列表)
2.委托实例(参数列表)
3.this.Invoke(委托实例, new object[] { 参数列表});
更多详情:https://www.cnblogs.com/jixiaosa/p/10687068.html
二、事件 事件是一种专门的代表类型,主要用于消息或通知传递。事件只能从它们发布的类型调用,通常基于EventHandler委托,其中对象代表事件的发送方,
System.EventArgs派生类保存有关事件的数据。实现事件和回调方法都是基于委托(delegate)。
using System; namespace SimpleEvent { using System; /***********发布器类***********/ public class EventDemo { private string message; public delegate void PrintEventHandler(string args); public event PrintEventHandler PrintEvent; protected virtual void OnPrinted(string s) { if ( PrintEvent != null ) { PrintEvent(s); /* 事件被触发 */ }else { Console.WriteLine( "事件没被触发" ); Console.ReadKey(); /* 回车继续 */ } } public EventDemo() { PrintMessage("cjavapy"); } public void PrintMessage( string s ) { message=s; OnPrinted(s); } } /***********订阅器类***********/ public class SubscribEvent { public void Print(string s) { Console.WriteLine( "触发事件" ); Console.WriteLine( s ); Console.ReadKey(); /* 回车继续 */ } } /***********触发***********/ public class MainClass { public static void Main() { EventDemo e = new EventDemo(); /* 实例化对象,第一次没有触发事件 */ SubscribEvent s = new SubscribEvent(); /* 实例化对象 */ e.PrintEvent += new EventDemo.PrintEventHandler(s.Print); /* 注册 */ e.PrintMessage("C#"); e.PrintMessage("Java"); } } }
分类:
C# / C#基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!