.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("----------------------------");
            // 使用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(11);
            Console.WriteLine("----------------------------");
            // 使用delegate
            Action<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("----------------------------");
            // 使用delegate
            Action<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}");
        }
    }
}
相关推荐
津津有味道6 小时前
ISO18000-6C协议UHF6C超高频RFID读写C#源码
c#·uhf6c·超高频·iso18000-6c
白雪公主的后妈6 小时前
Auto CAD二次开发——创建圆弧对象
c#·cad二次开发·创建圆弧对象
weixin_307779139 小时前
C#程序实现将MySQL的存储过程转换为Azure Synapse Dedicated SQL Pool的T-SQL存储过程
c#·自动化·云计算·运维开发·azure
"菠萝"10 小时前
C#知识学习-018(方法参数传递)
学习·c#·1024程序员节
CiLerLinux11 小时前
第三章 FreeRTOS 任务相关 API 函数
开发语言·单片机·物联网·c#
.NET修仙日记11 小时前
C#/.NET 微服务架构:从入门到精通的完整学习路线
微服务·c#·.net·.net core·分布式架构·技术进阶
歪歪10021 小时前
在C#中详细介绍一下Visual Studio中如何使用数据可视化工具
开发语言·前端·c#·visual studio code·visual studio·1024程序员节
Eiceblue21 小时前
如何通过 C# 高效读写 Excel 工作表
c#·visual studio·1024程序员节
张人玉21 小时前
WPF 触发器详解:定义、种类与示例
c#·wpf·1024程序员节·布局控件
追逐时光者1 天前
全面的 C#/.NET 图表构建解决方案,助力快速实现图表开发需求!
后端·.net