34
votes

I can't get the following to compile:

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

Can anyone point out what I'm doing wrong?

2

2 Answers

66
votes

You don't specify a return type when using anonymous methods. This would work:

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

Some alternatives:

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

Why not lambda notation?

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