C#中的Action<>和Func<> 委托区别
2013-04-18 22:23
507 查看
C#中的Action<>和Func<>
其实他们两个都是委托【代理】的简写形式。一、【action<>】指定那些只有输入参数,没有返回值的委托
Delegate的代码:
[csharp] view plaincopypublic 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 plaincopypublic static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
Action<string> action = HellowChinese;
action("Spring.");
就是相当于省去了定义委托的步骤了。
二、func<> 这个和上面的那个是一样的,区别是这个有返回值!
[csharp] view plaincopypublic static string HelloEnglish(string strEnglish)
{
return "Hello." + strEnglish;
}
Func<string, string> f = HelloEnglish;
Console.WriteLine(f("Srping ji"));
Console.ReadLine();
相关文章推荐
- C# Action<T>、Func<T>、Predicate<T>委托的区别、用法
- C#中的Action<>和Func<>以及传统的委托
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- C# 笔记 Func<TResult> 委托、Action<T> 委托
- c#中Action<T>和Func<T>委托
- C#的委托 Action<>和Func<>
- C#基础:委托之Action<T>和Func<T>的用法(转)
- c# action<> func<> 这2个委托怎么用和理解
- C#.NET的Linq查询、lambda、委托:Func<>和Action
- C#基础:委托之Action<T>和Func<T>的用法
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- C#中的Action<T>、Func<T>委托和Lambda表达式
- c# action<> func<> 这2个委托怎么用和理解
- C#的委托 Action<>和Func<>
- 【基础】C#:委托Action、Action<T>、Func<T>、Predicate<T>
- [转]C#委托Action、Action<T>、Func<T>、Predicate<T>
- 提问c# action<> func<> 这2个委托怎么用和理解
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- C#基础:委托之Action<T>和Func<T>的用法
- C# Func<T>、Action<T> 的区别于说明