C#中Action的用法

Action 是 C# 中委托的一种,用于封装无返回值的方法。它引用的方法不能有返回值,但可以有零个或多个参数。相比delegate委托,Action 委托的优点是不必显式定义封装无参数过程的委托,使代码更加简洁和易读。

1、delegate-委托

先简单介绍一下关键字delegate

(1)delegate用来定义一种变量类型,特别的地方在于这种类型只能指向方法。两个关键点:①输入参数(个数、类型、顺序);②返回值。

(2)delegate的例子

using System;

namespace DelegateDemo
{
	delegate void MyDelegate(string s);//1、委托类型的定义:指向输入参数是字符串且无返回值的方法
	class Program
	{
		static void Method(string s)//注意:委托引用的函数与委托类型的定义必须是一样的
		{
			Console.WriteLine($"hello {s}");
		}
		static void Main(string[] args)
		{
			MyDelegate myDele = new MyDelegate(Method); //2、委托的实例化

			myDele("myDele"); //3、委托的两种调用方式
			myDele.Invoke("myDele.Invoke");

			Console.Read();
		}
	}
}

输出

hello myDele
hello myDele.Invoke

(3)多播委托

delegate的妙用-多播委托,此时会按顺序依次调用引用的所有方法,下面是多播委托的例子

using System;

namespace DelegateDemo
{
	delegate void MyDelegate(string s);
	class Program
	{
		static void Method1(string s)
		{
			Console.WriteLine($"Method1 hello {s}");
		}
		static void Method2(string s)
		{
			Console.WriteLine($"Method2 hello {s}");
		}
		static void Method3(string s) =>//"=>"用于定义只有一句的方法
			Console.WriteLine($"Method3 hello {s}");
		static void Main(string[] args)
		{
			MyDelegate myDele = new MyDelegate(Method1); 
			myDele += Method2;//多播委托-增加引用
			myDele("myDele"); 
			Console.WriteLine($"-------1");
			myDele += Method2;//多播委托-增加引用
			myDele("myDele");
			Console.WriteLine($"-------2");
			myDele += Method3;
			myDele("myDele");
			Console.WriteLine($"-------3");
			myDele -= Method2;//多播委托-删除引用
			myDele("myDele");

			Console.Read();
		}
	}
}

输出

Method1 hello myDele
Method2 hello myDele
-------1
Method1 hello myDele
Method2 hello myDele
Method2 hello myDele
-------2
Method1 hello myDele
Method2 hello myDele
Method2 hello myDele
Method3 hello myDele
-------3
Method1 hello myDele
Method2 hello myDele
Method3 hello myDele

下面通过两个例子来说明Action委托的用法。

2、Action无参数的例子

using System;

namespace ActionDemo1
{
    delegate void MyDelegate();            //1、委托类型   
    class Program
    {
		static void Method()
		{
			Console.WriteLine("Method 被调用" );
		}
		static void Main(string[] args)
        {
			MyDelegate myDel = new MyDelegate(Method);//2、委托实例化
            myDel();//3、委托对象的调用

			Action showActioMethod = Method;
			showActioMethod();

			Console.ReadLine();
        }
    }
}

输出

Method 被调用
Method 被调用

3、Action有参数的例子

using System;

namespace ActionDemo2
{
    delegate void MyDelegate(string s);          //1、定义委托类型    
    class Program
    {
		static void Method(string s) 
		{
			Console.WriteLine("Method 被调用,输入:" + s);
		}
		static void Main(string[] args)
        {
			MyDelegate myDel = new MyDelegate(Method);//2、委托实例化
            myDel("delegate委托");//3、委托对象的调用

			Action<string> showActioMethod = Method;
			showActioMethod("Action委托");

			Console.ReadLine();
        }
    }
}

 输出

Method 被调用,输入:delegate委托
Method 被调用,输入:Action委托

可以看到,使用 delegate 进行委托操作通常需要三步,而使用 Action 进行委托时,只需两步即可完成。尤其是无需提前定义委托类型这一点,非常关键。这样,在阅读代码时,我们不需要跳转到委托类型的定义部分,从而提升了代码的可读性和简洁性。

参考

C#内置泛型委托:Action委托 - .NET开发菜鸟 - 博客园

Action 委托 (System) | Microsoft Learn

叩响C#之门 (16.2 多播委托)(豆瓣)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值