内联委托声明(c#)

时间:2011-01-27 11:08:56

标签: c# delegates

我无法得到以下内容进行编译:

var x = new Action(delegate void(){});

有谁可以指出我做错了什么?

2 个答案:

答案 0 :(得分:56)

使用匿名方法时,不指定返回类型。这可行:

var x = new Action(delegate(){});

一些替代方案:

Action x = () => {}; // Assuming C# 3 or higher
Action x = delegate {};
Action x = delegate() {};
var x = (Action) (delegate{});

答案 1 :(得分:17)

为什么不用lambda表示法?

Action myAction= (Action)(()=>
{
});
相关问题