简体   繁体   English

设置枚举参数的默认值

[英]Setting the default value for enum parameter

I have a method that accepts an enum value as an input parameter.我有一个接受枚举值作为输入参数的方法。 How do I set the default value for this enum param?如何设置此枚举参数的默认值?

Below code shows how I set it now but if no value defined while calling this method, it's not using the default value.下面的代码显示了我现在如何设置它,但如果在调用此方法时未定义任何值,则它不使用默认值。 It uses another value.它使用另一个值。 Any idea why?知道为什么吗?

public string MyFunction(string param1, MyEnum param2 = MyEnum.MyDefaultValue)
{
   // Do something...
}

I call this function without specifying a value for param2 -- see below:我调用这个函数时没有为param2指定一个值——见下文:

... // Some code
var response = MyFunction("Hello World!"); // Purposely not specifying the param2 value so it uses the default one

When I hit my break point in MyFunction after calling it without specifying a value for param2 , I see that it does NOT use the default value.当我在调用MyFunction后在没有指定param2值的情况下点击它的断点时,我看到它不使用默认值。

Any idea why?知道为什么吗?

根据评论,事实证明这是在接口中设置的不同默认值,并且它是由依赖注入器应用的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM