C# 多个可选参数调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17728791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:20:09  来源:igfitidea点击:

Multiple optional parameters calling function

c#functionoptional-parametersoptional-argumentsoptional-variables

提问by MonsterMMORPG

Assume that i have a function like this below It takes 3 parameters and 2 have optional values

假设我有一个像下面这样的函数它需​​要 3 个参数,2 个有可选值

private void  myfunc (int a, int b=2, int c=3)
{
  //do some stuff here related to a,b,c
}

now i want to call this function like below how possible ?

现在我想像下面这样调用这个函数怎么可能?

myfunc(3,,5)

So i want it to use default parameter b=2

所以我希望它使用默认参数 b=2

But it is giving error that way.

但它以这种方式给出错误。

Here the error message

这里的错误信息

Argument missing

C# 4.5

C# 4.5

采纳答案by It'sNotALie.

You need to use named parameters, like so:

您需要使用命名参数,如下所示:

myfunc(a, c:5);

回答by jltrem

call it like this:

像这样称呼它:

myfunc(3, c: 5)

You can read up on named parameters on MSDN. Named parameters can be in any order but must follow positional parameters; i.e., once you use a named parameter you cannot use a positional parameter.

您可以阅读MSDN上的命名参数。命名参数可以按任何顺序排列,但必须遵循位置参数;即,一旦使用命名参数,就不能使用位置参数。