您的位置:首页 > 编程语言 > C#

c# 中的Action<> 和 Func<>

2014-10-27 18:13 183 查看
原文链接:http://www.2cto.com/kf/201301/186760.html

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

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

Delegate的代码:

[csharp]
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]
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}

Action<string> action = HellowChinese;
action("Spring.");
就是相当于省去了定义委托的步骤了。

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

[csharp] www.2cto.com
public static string HelloEnglish(string strEnglish)
{
return "Hello." + strEnglish;
}

Func<string, string> f = HelloEnglish;
Console.WriteLine(f("Srping ji"));
Console.ReadLine();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: