什么是动作<串GT ;?

问题描述:

什么是动作&LT;串&GT; ,怎么能使用

动作是有一个有4个参数(在.NET 4月16日),并没有返回值标准的代表。它是用来表示一个动作

Action is a standard delegate that has one to 4 parameters (16 in .NET 4) and doesn't return value. It's used to represent an action.

Action<String> print = (x) => Console.WriteLine(x);

List<String> names = new List<String> { "pierre", "paul", "jacques" };
names.ForEach(print);

有其他预定义的代表:


  • 谓词,有一个参数,并返回一个布尔委托。

  • Predicate, delegate that has one parameter and returns a boolean.

Predicate<int> predicate = ((number) => number > 2);
var list = new List<int> { 1, 1, 2, 3 };
var newList = list.FindAll(predicate);


  • 函数功能是比较通用的之一,它具有1到4个参数(在.NET 4月16日),返回的东西

  • Func is the more generic one, it has 1 to 4 parameters (16 in .NET 4) and returns something