当前位置: 移动技术网 > IT编程>开发语言>.net > Func与Action

Func与Action

2018年12月18日  | 移动技术网IT编程  | 我要评论

新欢找上门,波思宇羽绒服,如果爱忘了歌曲链接

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

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 }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网