委托学习小案例三——Action<>
委托学习小案例三——Action<>
class Program { static void Main(string[] args) { //无参数无返回值的委托 Action action1 = new Action(ActionWithNoParaNoReturn); action1(); Console.WriteLine("----------------------------"); // 使用delegate Action action2 = delegate { Console.WriteLine("这里是使用delegate"); }; // 执行 action2(); Console.WriteLine("----------------------------"); // 使用匿名委托 Action action3 = () => { Console.WriteLine("这里是匿名委托"); }; action3(); Console.WriteLine("----------------------------"); // 有参数无返回值的委托 Action<int> action4 = new Action<int>(ActionWithPara); action4(23); Console.WriteLine("----------------------------"); // 使用delegate Action<int> action5 = delegate (int i) { Console.WriteLine($"这里是使用delegate的委托,参数值是:{i}"); }; action5(45); Console.WriteLine("----------------------------"); // 使用匿名委托 Action<string> action6 = (string s) => { Console.WriteLine($"这里是使用匿名委托,参数值是:{s}"); }; action6("345"); Console.WriteLine("----------------------------"); // 多个参数无返回值的委托 Action<int, string> action7 = new Action<int, string>(ActionWithMulitPara); action7(7, "abc"); Console.WriteLine("----------------------------"); // 使用delegate,Action就是无返回值的委托 Action<int, int, string> action8 = delegate (int i1, int i2, string s) { Console.WriteLine($"这里是三个参数的Action委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s}"); }; action8(12, 34, "abc"); Console.WriteLine("----------------------------"); Action<int, int, string, string> action9 = (int i1, int i2, string s1, string s2) => { Console.WriteLine($"这里是使用四个参数的委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s1},参数4的值是:{s2}"); }; // 执行委托 action9(34, 56, "abc", "def"); Console.ReadKey(); } #region Method //定义无参数的引用方法 static void ActionWithNoParaNoReturn() { Console.WriteLine("这是无参数无返回值的Action委托"); } //定义单个参数的引用方法 static void ActionWithPara(int i) { Console.WriteLine($"这里是有参数无返回值的委托,参数值是:{i}"); } //定义2个参数的引用方法 static void ActionWithMulitPara(int i, string s) { Console.WriteLine($"这里是有两个参数无返回值的委托,参数1的值是:{i},参数2的值是:{s}"); } #endregion }
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/14769084.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2020-05-14 C#的实现FTP传送文件