我粘贴了一些来自Jon Skeet的C# In Depth站点的代码:
static void Main()
{
// First build a list of actions
List<Action> actions = new List<Action>();
for (int counter = 0; counter < 10; counter++)
{
actions.Add(() => Console.WriteLine(counter));
}
// Then execute them
foreach (Action action in actions)
{
action();
}
} http://csharpindepth.com/Articles/Chapter5/Closures.aspx
请注意这一行:
actions.Add( () )
括号里的()是什么意思?
我见过几个lambda表达式、委托、Action对象的使用等示例,但我还没有看到对此语法的解释。是干什么的呢?为什么需要它?
发布于 2009-02-26 14:28:42
这是声明一个不带参数的lambda表达式的简写。
() => 42; // Takes no arguments returns 42
x => 42; // Takes 1 argument and returns 42
(x) => 42; // Identical to above发布于 2009-02-26 14:27:39
这是一个不带参数的lambda表达式。
发布于 2009-02-26 14:30:06
来自MSDN。表达式lambda采用(输入)=>expression的形式。因此,像()=>expression这样的λ表示没有输入参数。哪个动作的签名不带参数
https://stackoverflow.com/questions/590756
复制相似问题