Bug - Optional parameter with other parameters using params not working properly #6815
-
Hello everyone! Recently we were discussing one behavior on dotnet/docs about this issue. I want to ask which approach we should commit to. We find an error about the You can see some examples below: This example compiles but the Default value parameter is never used: Example 1 This forces the usage of the This forces the usage of the Using these examples, we came up with the following suggestions: 1 - Take off the possibility to have an optional parameter with other parameters using params. One or another should be used never both. Possibly creating a new compiler error to be triggered when that happens. 2 - Correct the behavior to accept an optional parameter and put them at the end (CS0231 must add an exception to the optional parameter). Today we have a rule that params should be the last one. We may add an exception for this rule when using an optional parameter -or- 3 - Correct the behavior to accept an optional parameter to be understood with a param key (IDE0060 it's not thrown when the param is the last one). This is what's happening today. The optional parameter it's not being identified when we are trying to use it together with one parameter using the key parameters no matter the types we are using. So we need to correct the behavior to understand the parameter with default values, even if it's not the last one when it is together with another parameter params. There was a discussion about but they never assigned the problem or even propose this as a bug. Discussion |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@Viniciusap I'm not sure where the bug is, or what your proposed solutions mean. We're not going to change the language to remove the ability to mix optional parameters and params, that's been there since the feature was introduced and there are plenty of existing apis that use it (and you can even manually put the attributes on parameters to create things like optional parameters in the middle of a parameter list, which the computer understands even though you can't use the syntax directly). I certainly think there could be an analyzer, but this repo isn't the place for that, as Bill mentioned on your docs issue. |
Beta Was this translation helpful? Give feedback.
@Viniciusap I'm not sure where the bug is, or what your proposed solutions mean. We're not going to change the language to remove the ability to mix optional parameters and params, that's been there since the feature was introduced and there are plenty of existing apis that use it (and you can even manually put the attributes on parameters to create things like optional parameters in the middle of a parameter list, which the computer understands even though you can't use the syntax directly). I certainly think there could be an analyzer, but this repo isn't the place for that, as Bill mentioned on your docs issue.