码迷,mamicode.com
首页 > Windows程序 > 详细

C#中的Action<>和Func<>

时间:2015-03-19 13:09:08      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

其实他们两个都是委托【代理】的简写形式。

一、【action<>】指定那些只有输入参数,没有返回值的委托

Delegate的代码:

[csharp] view plaincopy
 
  1. public delegate void myDelegate(string str);  
  2. public static void HellowChinese(string strChinese)  
  3. {  
  4.     Console.WriteLine("Good morning," + strChinese);  
  5.     Console.ReadLine();  
  6. }  
  7.   
  8. myDelegate d = new myDelegate(HellowChinese);  
  9. d("Mr wang");  


用了Action之后呢:

 

[csharp] view plaincopy
 
  1. public static void HellowChinese(string strChinese)  
  2. {  
  3.     Console.WriteLine("Good morning," + strChinese);  
  4.     Console.ReadLine();  
  5. }  
  6.   
  7. Action<string> action = HellowChinese;  
  8. action("Spring.");  

就是相当于省去了定义委托的步骤了。

 

二、func<> 这个和上面的那个是一样的,区别是这个有返回值!

[csharp] view plaincopy
 
    1. public static string HelloEnglish(string strEnglish)  
    2. {  
    3.     return "Hello." + strEnglish;  
    4. }  
    5.   
    6. Func<string, string> f = HelloEnglish;  
    7. Console.WriteLine(f("Srping ji"));  
    8. Console.ReadLine();  

C#中的Action<>和Func<>

标签:

原文地址:http://www.cnblogs.com/zhepama/p/4349974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!