-->

Does optional parameter and optional attribute not

2020-05-09 22:32发布

问题:

public void ObjTest(StringBuilder sb, List<string> list, int i = 0,  [Optional] string bs)
{
    ......
 }

The above code throwing compilation error " Optional parameters must appear after all required parameters ". Does optional parameter and optional attribute not supported together in same method parameter, but it allows params arry after optional paramer ?

回答1:

You can use them in conjunction but the optional parameter (the language construct) must be the last parameter in the parameter list.

public void X(StringBuilder sb, List<string> list, [Optional] string bs, int i = 0)
{
}