当前位置: 首页 > news >正文

.NET Framework中自带的泛型委托Action

Action<>是.NET Framework中自带的泛型委托,可以接收一个或多个输入参数,但不返回任何参数,可传递至多16种不同类型的参数类型。在Linq的一些方法上使用的比较多。

 

1、Action泛型委托

.NET Framework为我们提供了多达16个参数的Action委托定义,对于常见的开发场景已经完全够用。

如下图,

示例说明:

Action<>:委托至少0个参数,至多16个参数,无返回值。

Action:表示无参,无返回值的委托。

Action :表示有传入参数int,string无返回值的委托。

Action:表示有传入参数int,string,bool无返回值的委托。

Action:表示有传入4个int型参数,无返回值的委托。

2、Action泛型委托的使用

Action泛型委托的使用方法,可以通过下面代码看一下,

例如,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ActionDemo
{class Program{static void Main(string[] args){// 无参数无返回值的委托Action action1 = new Action(ActionWithNoParaNoReturn);action1();Console.WriteLine("----------------------------");// 使用delegateAction action2 = delegate { Console.WriteLine("使用delegate"); };// 执行action2();Console.WriteLine("----------------------------");// 使用匿名委托Action action3 = () => { Console.WriteLine("匿名委托"); };action3();Console.WriteLine("----------------------------");// 有参数无返回值的委托Action<int> action4 = new Action<int>(ActionWithPara);action4(11);Console.WriteLine("----------------------------");// 使用delegateAction<int> action5 = delegate (int i) { Console.WriteLine($"使用delegate的委托,参数值是:{i}"); };action5(22);Console.WriteLine("----------------------------");// 使用匿名委托Action<string> action6 = (string s) => { Console.WriteLine($"使用匿名委托,参数值是:{s}"); };action6("C#");Console.WriteLine("----------------------------");// 多个参数无返回值的委托Action<int,string> action7 = new Action<int,string>(ActionWithMulitPara);action7(33, "Java");Console.WriteLine("----------------------------");// 使用delegateAction<int,int,string> action8 = delegate (int i1, int i2, string s) {Console.WriteLine($"三个参数的Action委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s}");};action8(44, 55, "Python");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(66,77, "C","CJavaPy");Console.ReadKey();}static void ActionWithNoParaNoReturn(){Console.WriteLine("无参数无返回值的Action委托");}static void ActionWithPara(int i){Console.WriteLine($"有参数无返回值的委托,参数值是:{i}");}static void ActionWithMulitPara(int i,string s){Console.WriteLine($"有两个参数无返回值的委托,参数1的值是:{i},参数2的值是:{s}");}}
}

 

http://www.lryc.cn/news/222771.html

相关文章:

  • DAIR-V2X-V 3D检测数据集 转为Kitti格式 | 可视化
  • 深入理解指针:【探索指针的高级概念和应用二】
  • 腾讯觅影数智医疗影像平台获颁世界互联网领先科技成果大奖
  • 鸿蒙开发工具DevEco Studio的下载和安装
  • 【原理篇】四、自定义starter
  • redisTemplate不支持zpopmax,解决方案使用reverseRangeWithScore
  • 基于深度模型的日志异常检测
  • 最大连续子数组
  • 【FastCAE源码阅读5】使用VTK实现鼠标拾取对象并高亮
  • 【全志H616 使用标准库 完成自制串口库(分文件实现) orangepi zero2(开源)】.md updata: 23/11/07
  • 小白学爬虫:手机app分享商品短连接获取淘宝商品链接接口|淘宝淘口令接口|淘宝真实商品链接接口|淘宝商品详情接口
  • python 应用之 request 请求调用
  • BeanUtils.copyProperties浅拷贝的坑你得知道?
  • ubuntu安装rabbitMQ 并 开启记录消息的日志
  • 思维模型 首因效应
  • Redis极速上手开发手册【Redis全面复习】
  • [动态规划] (十四) 简单多状态 LeetCode LCR 091.粉刷房子
  • 【VSS版本控制工具】
  • 数据持久化技术(Python)的使用
  • 第23章(上)_索引原理之索引与约束
  • 金蝶云星空BOS设计器中基础资料字段属性“过滤”设置获取当前界面的基础资料值作为查询条件
  • OFDM深入学习及MATLAB仿真
  • 软件测试简历原来是写了这些才让面试官已读不回
  • ESP32网络开发实例-Web服务器RGB LED调光
  • C# TCP Server服务端多线程监听RFID读卡器客户端上传的读卡数据
  • 【electron】【附排查清单】记录一次逆向过程中,fetch无法请求http的疑难杂症(net::ERR_BLOCKED_BY_CLIENT)
  • 【JS】scrollTop+scrollHeight+clientTop+clientHeight+offsetTop+offsetHeight
  • Go语言函数用法
  • 3.5、Linux:命令行git的使用
  • 基于servlet+jsp+mysql网上书店系统