内联委托声明(c#)

内联委托声明(c#),c#,delegates,C#,Delegates,我无法编译以下内容: var x = new Action(delegate void(){}); 有人能指出我做错了什么吗?使用匿名方法时不指定返回类型。这将有助于: var x = new Action(delegate(){}); 一些备选方案: Action x = () => {}; // Assuming C# 3 or higher Action x = delegate {}; Action x = delegate() {}; var x = (Action) (de

我无法编译以下内容:

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

有人能指出我做错了什么吗?

使用匿名方法时不指定返回类型。这将有助于:

var x = new Action(delegate(){});
一些备选方案:

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

为什么不使用lambda符号

Action myAction= (Action)(()=>
{
});

@maxp,您还可以使用语法,例如
Action x=delegate(){}-两者相同,因此可根据您的喜好使用@leppie:我也不喜欢,但这是使OP的代码编译所需的最小更改:)我将提供一些替代方案…可以简化为
Action myAction=()=>{}