Jul*_*rau 39
Action
是一个标准委托,有一到四个参数(在.NET 4中为16)并且不返回值.它用于表示动作.
Action<String> print = (x) => Console.WriteLine(x);
List<String> names = new List<String> { "pierre", "paul", "jacques" };
names.ForEach(print);
Run Code Online (Sandbox Code Playgroud)
还有其他预定义的委托:
Predicate
,委托有一个参数并返回一个布尔值.
Predicate<int> predicate = ((number) => number > 2);
var list = new List<int> { 1, 1, 2, 3 };
var newList = list.FindAll(predicate);
Run Code Online (Sandbox Code Playgroud)Func
是更通用的,它有1到4个参数(在.NET 4中为16)并返回一些东西
Rub*_*ben 10
这是具有签名的函数的委托
void Bla(string parameter)
.您可以使用它将函数传递给其他函数.例如,你可以这样做
Action<string> action = (x => Console.WriteLine(x));
new List<string>{"1","2","3"}.ForEach(action);
Run Code Online (Sandbox Code Playgroud)
将所有字符打印到控制台