开发者

Inline delegate declaration (c#)

开发者 https://www.devze.com 2023-02-07 14:02 出处:网络
I can\'t get the following to comp开发者_运维技巧ile: var x = new Action(delegate void(){}); Can anyone point out what I\'m doing wrong?You don\'t specify a return type when using anonymous method

I can't get the following to comp开发者_运维技巧ile:

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

Can anyone point out what I'm doing wrong?


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{});


Why not lambda notation?

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号