c#默认参数

Chi*_*hin 17 .net c#

在其他语言中,我可以设置方法签名

cookEgg(boolean hardBoiled = true)
Run Code Online (Sandbox Code Playgroud)

如果我在方法调用中没有收到参数,则默认将参数hardboiled设置为true.我如何在c#中实现这一目标?

非常感谢

Pav*_*aev 31

目前,您必须重载该方法:

void cookEgg(bool hardBoiled) { ... }
void cookEgg() { cookEgg(true); }
Run Code Online (Sandbox Code Playgroud)

C#4.0将添加可选参数 - 您将能够像在原始示例中一样编写代码,并且它将按照您的预期工作.