Func与Action

平时我们如果要用到委托一般都是先声明一个委托类型,比如:

1
private delegate string Say();                                   

string说明适用于这个委托的方法的返回类型是string类型,委托名Say后面没有参数,说明对应的方法也就没有传入参数。

写一个适用于该委托的方法:

1 public static string SayHello()                                      
2 {                                                                    
3     return "Hello";                                                  
4 }                                                               

最后调用:

1 static void Main(string[] args)                                      
2 {                                                                    
3     Say say = SayHello;                                              
4     Console.WriteLine(say());                                        
5 } 

                                                             

这里我们先声明委托,然后再将方法传给该委托。有没有办法可以不定义委托变量呢?

答案是肯定的,我们可以用Func.

Func是.NET里面的内置委托,它有很多重载。

Func<TResult>:没有传入参数,返回类型为TResult的委托。就像我们上面的Say委托,就可以用Func<string>来替代,调用如下:

1 static void Main(string[] args)                                      
2 {                                                                    
3      Func<string> say = SayHello;                                    
4      //Say say = SayHello;                                           
5      Console.WriteLine(say());                                       
6 }

                                                                

怎么样,有了Func很简单吧。看一下Func别的重载。

Func<T, TResult> 委托:有一个传入参数T,返回类型为TResult的委托。如:

1 //委托 传入参数类型为string,方法返回类型为int                       
2 Func<string, int> a = Count;                                         
3 //对应方法                                                           
4 public int Count(string num)                                         
5 {                                                                    
6     return Convert.ToInt32(num);                                     
7 }

                                                

Func<T1, T2, TResult> 委托:有两个传入参数:T1T2,返回类型为TResult

类似的还有Func(T1, T2, T3, TResult) 委托、Func(T1, T2, T3, T4, TResult) 委托等。用法差不多,都是前面为方法的传入参数,最后一个为方法的返回类型。

Func也可以与匿名方法一起使用如:

复制代码
 1 public static void Main()                                            
 2 {                                                                    
 3      Func<string, int, string[]> extractMeth = delegate(string s, int i)
 4      {                                                               
 5            char[] delimiters = new char[] { ' ' };                   
 6            return i > 0 ? s.Split(delimiters, i) : s.Split(delimiters);
 7      };                                                              
 8                                                                      
 9      string title = "The Scarlet Letter";                            
10      // Use Func instance to call ExtractWords method and display result
11      foreach (string word in extractMeth(title, 5))                  
12             Console.WriteLine(word);                                 
13 }    
复制代码

 

同样它也可以接 lambda 表达式

 

复制代码
 1 public static void Main()                                            
 2 {                                                                    
 3     char[] separators = new char[] {' '};                            
 4     Func<string, int, string[]> extract = (s, i) =>                  
 5            i > 0 ? s.Split(separators, i) : s.Split(separators) ;    
 6                                                                      
 7     string title = "The Scarlet Letter";                             
 8     // Use Func instance to call ExtractWords method and display result
 9     foreach (string word in extract(title, 5))                       
10          Console.WriteLine(word);                                    
11 }
复制代码

 

Func都是有返回类型的,如果我们的方法没有返回类型该怎么办呢?铛铛铛,这时Action就要粉墨登场了。

Action 委托:没有传入参数,也没有返回类型,即Void。如:

复制代码
1 static void Main(string[] args)                                      
2 {                                                                    
3      Action say = SayHello;                                          
4      say();                                                          
5 }                                                                    
6 public static void SayHello( )                                       
7 {                                                                    
8      Console.WriteLine("Say Hello");                                 
9 }    
复制代码

 

Action<T> 委托:传入参数为T,没有返回类型。如:

 

复制代码
1 static void Main(string[] args)                                      
2 {                                                                    
3      Action<string> say = SayHello;                                  
4      say("Hello");                                                   
5 }                                                                    
6 public static void SayHello(string word )                            
7 {                                                                    
8      Console.WriteLine(word);                                        
9 }
复制代码

 

                                                                  

Action<T1, T2> 委托:两个传入参数,分别为T1T2,没有返回类型。

Action同样的还有许多其它重载,每个重载用法一样,只是方法的传入参数数量不一样。

其实ActionFunc的用法差不多,差别只是一个有返回类型,一个没有返回类型,当然Action也可以接匿名方法和Lambda表达式。

匿名方法:

 

static void Main(string[] args)                                      
{                                                                    
      Action<string> say = delegate(string word)                     
      {                                                              
           Console.WriteLine(word);                                  
      };                                                             
      say("Hello Word");                                             
}

 

Lambda表达式:

1 static void Main(string[] args)                                      
2 {                                                                    
3       Action<string> say = s => Console.WriteLine(s);                
4       say("Hello Word");                                             
5 }

 

原文<https://www.cnblogs.com/BrokenIce/p/5823798.html>

posted @   linjierd  阅读(323)  评论(3编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示