Action<>和Func<> 委托【代理】

本文详细介绍了C#中的Action和Func两种泛型委托的使用方法。Action用于无返回值的方法,Func则用于有返回值的方法。通过示例代码展示了如何使用这两种委托简化代码,并提高编码效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#中的Action<>和Func<> 其实他们两个都是委托【代理】的简写形式。
一、【action<>】指定那些只有输入参数,没有返回值的委托
Delegate的代码:
[csharp] view plain copy print?
public delegate void myDelegate(string str);  
public static void HellowChinese(string strChinese)  
{  
     Console.WriteLine("Good morning," + strChinese);  
     Console.ReadLine();  
}  
  
myDelegate d = new myDelegate(HellowChinese);  
d("Mr wang");  

用了Action之后呢:

 
[csharp] view plain copy print?
public static void HellowChinese(string strChinese)  
{  
     Console.WriteLine("Good morning," + strChinese);  
     Console.ReadLine();  
}  
  
Action action = HellowChinese;  
action("Spring.");  
就是相当于省去了定义委托的步骤了。
 
二、func<> 这个和上面的那个是一样的,区别是这个有返回值!
[csharp] view plain copy print?
public static string HelloEnglish(string strEnglish)  
{  
     return "Hello." + strEnglish;  
}  
  
Func<string, string> f = HelloEnglish;  
Console.WriteLine(f("Srping ji"));  
Console.ReadLine();  
------------------------------------------------------------------------------------------------------------------------------

本节学习了泛型委托Action<T>和Fun<TResult>两类特殊的委托,这两个特殊的委托是Dot FrameWrok自带的。结合lambda表达式,可以在写程序时,简洁代码和提高编码效率。
(一)Action<T>和Fun<TResult>两个委托的不同点:
Action<T>只能委托必须是无返回值的方法
Fun<TResult>只是委托必须有返回值的方法
(二)代码演练
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//方法一:显式声明了一个委托,并将对 实例方法的引用分配给其委托实例。
publicdelegatevoid ShowName();
publicdelegatevoid ShowNameWithParameter(string name);
publicdelegatevoid ShowAge(int age);
publicdelegatevoid ShowNameAndAge(string name, int age);
publicdelegateint ReturnName(string name);
namespace ActionDemo
{ 
publicclass Person
{ 
privatestring instanceName;

publicstring InstanceName
{
get { return instanceName; }
set { instanceName = value; }
}
privateint instanceAge;

publicint InstanceAge
{
get { return instanceAge; }
set { instanceAge = value; }
}
public Person(string name,int age)
{
this.instanceName = name;
this.instanceAge = age;
}
publicvoid DisplayName()
{
Console.WriteLine("Name:{0}",this.instanceName);
}
publicvoid DisplayName(string name)
{
Console.WriteLine("Name:{0}",name);
} 
publicvoid DisplayAge(int age)
{
Console.WriteLine("Age:{0}",age);
}
publicvoid DisplayNameAndAge(string name, int age)
{
Console.WriteLine(string.Format("Name:{0} And Age:{1} ",name,age));
}
publicint GetAgeByName(string name)
{
if(name==instanceName)
{
return instanceAge;
}
else
{
return-1;
}

}

}
class Program
{
privatestaticvoid Print(string s)
{
Console.WriteLine(s);
}


staticvoid Main(string[] args)
{
#region Action<T>相关
Person person =new Person("joetao",21);
//非泛型委托
//ShowName showName = new ShowName(name.DisplayName);
//另一种写法
ShowName showName = person.DisplayName;
showName();
ShowNameWithParameter showNameWithParameter = person.DisplayName;
showNameWithParameter(person.InstanceName);
ShowAge showAge = person.DisplayAge;
showAge(person.InstanceAge);
ShowNameAndAge showNameAndAge = person.DisplayNameAndAge;
showNameAndAge(person.InstanceName, person.InstanceAge);

//泛型委托Action<T,...>这里指多个类型参数
//Action<T,..>委托:只能委托无返回值(void)的方法。
//(2)可以使用此委托以参数形式传递方法,而不用显式声明自定义的委托 
Action actionShowName = person.DisplayName;
actionShowName();
Action<string> actionShowName1 = person.DisplayName;
actionShowName1(person.InstanceName);
Action<int> actionShowAge = person.DisplayAge;
actionShowAge(person.InstanceAge);
Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge;
actionShowNameAndAge(person.InstanceName, person.InstanceAge);

//匿名泛型委托
//(1)初步
Action<string> actionPrintNameAtFirst;
actionPrintNameAtFirst =delegate(string s) { Console.WriteLine("Name:{0}",s); };
actionPrintNameAtFirst(person.InstanceName);
//(2)简化
Action<string> actionprintName;
actionprintName = s => Console.WriteLine("Name:{0}", s);
actionprintName(person.InstanceName);

//示例演示如何使用 Action<T> 委托来打印 List<T> 对象的内容
//使用 Print 方法将列表的内容显示到控制台上。 此外,C# 示例还演示如何使用匿名方法将内容显示到控制台上
//请注意该示例不显式声明 Action<T> 变量。 相反,它传递方法的引用,该方法采用单个参数而且不将值返回至 List<T>.ForEach 方法,其单个参数是一个 Action<T> 委托
// 同样,在 C# 示例 中,Action<T> 委托不被显式地实例化,因为匿名方法的签名匹配 List<T>.ForEach 方法所期望的 Action<T> 委托的签名。
List<String> names =new List<String>();
names.Add("Bruce");
names.Add("Alfred");
names.Add("Tim");
names.Add("Richard"); 
names.ForEach(Print); 
names.ForEach(delegate(String name){Console.WriteLine(name);});

#endregion

#region Fun<TResultf>相关
//泛型委托Func<T,...,TResult> 委托,这里可以有一个或多个或者没有参数T,但必须有返回值并返回 TResult 参数所指定的类型的值
//(1)必须有指定参数返回值。
//(2)Lambda 表达式的基础类型是泛型 Func 委托之一。 这样能以参数形式传递 lambda 表达式,而不用显式将其分配给委托。
//(3)因为 System.Linq 命名空间中许多类型方法具有 Func<T, TResult> 参数,因此可以给这些方法传递 lambda 表达式,而不用显式实例化 Func<T, TResult> 委托。

//非泛型委托实现
ReturnName returnName = person.GetAgeByName;
Console.WriteLine("Age:{0}", returnName(person.InstanceName));

//泛型委托实现
Func<string, int> funReturnName = person.GetAgeByName;
Console.WriteLine("Age:{0}",funReturnName(person.InstanceName));

//泛型匿名委托实现
//(1)初步
Func<string, int> funReturnName1 =delegate(string s) {return person.GetAgeByName(s); };
Console.WriteLine("Age:{0}", funReturnName1(person.InstanceName));
//(2)优化
Func<string, int> funReturnName2 = s => person.GetAgeByName(s);
Console.WriteLine("Age:{0}", funReturnName2(person.InstanceName));

//示例演示如何声明和使用 Func<T, TResult> 委托
//此示例声明一个 Func<T, TResult> 变量,并为其分配了一个将字符串中的字符转换为大写的 lambda 表达式。
//随后将封装此方法的委托传递给 Enumerable.Select 方法,以将字符串数组中的字符串更改为大写。 
Func<string, string> selector = str => str.ToUpper(); 
string[] words = { "orange", "apple", "Article", "elephant" }; 
IEnumerable<String> aWords = words.Select(selector);
foreach (String word in aWords)
Console.WriteLine(word);

#endregion
Console.ReadKey(); 
}
}
}

转载于:https://www.cnblogs.com/bb-love-dd/p/6014785.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值