Short Version: A named argument following an out
argument gives a compiler error, but I cannot find any support for this behaviour in the language specification.
Long Version:
I'm using the Enum.TryParse<TEnum>
three parameter overload, but I would prefer to name the ignoreCase
parameter to make my code clearer, a call like:
MyEnum res;
b = Enum.TryParse<MyEnum>(inputString, true, out res);
leaves the meaning of the boolean unclear (unless this method is known1). Hence I would like to use:
b = Enum.TryParse<MyEnum>(inputString, out res, ignoreCase: true);
However the compiler reports this as an error:
Named argument 'ignoreCase' specifies a parameter for which a positional argument has already been given
and the IDE highlights the ignoreCase
parameter. VS2010 targeting .NET 4, and VS11 Beta targeting either 4 or 4.5 all give the same result. In all cases naming the out
parameter removes the error.
b = Enum.TryParse<MyEnum>(inputString, result: out res, ignoreCase: true);
I've tried this across a number of different methods (including avoiding generics)2, both from the framework and in my assembly: always the same result: an out
parameter followed by a named parameter gives an error.
I can see no reason for this error, and §7.5.1 Argument Lists of the C# Language Specification: Version 4.0 does not seem to provide any reason why an out
followed by a named parameter should give an error. The text of the error seems to support an interpretation as a bug: there is no positional argument which could be a valid match for ignoreCase
.
Is my reading of the specification wrong? Or is this a compiler bug?
C# 7.2 Update
This restriction on all named arguments have to follow positional arguments when calling was lifted with C# 7.2.
See https://learn.microsoft.com/en-gb/dotnet/csharp/whats-new/csharp-7-2#non-trailing-named-arguments.
1 Hence the advice in the Framework Design Guidelines to prefer enum
parameters.
2 Eg: given:
private static void TestMethod(int one, float two, out string three) {
three = "3333";
}
this this call also gives the same error on the named parameter unless the out
parameter is also named:
TestMethod(1, out aString, two: 1.0f);
When you pass an argument by name, you specify the argument's declared name followed by a colon and an equal sign ( := ), followed by the argument value.
With named parameters, it is usually possible to provide the values in any arbitrary order, since the name attached to each value identifies its purpose. This reduces the connascence between parts of the program. A few languages use named parameters but still require the parameters to be provided in a specific order.
Named parameters provides us the relaxation to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. Using named parameters in C#, we can put any parameter in any sequence as long as the name is there.
Create a function which takes in a number of arguments which are specified by name rather than (necessarily) position, and show how to call the function. If the language supports reordering the arguments or optionally omitting some of them, note this.
Named parameters do not allow you to "skip" positional arguments.
Your code is parsed as passing the first two arguments—value
and ignoreCase
, then passing ignoreCase
again.
It has nothing to do with the out
ness of the parameter.
You can fix it by passing the last parameter as named too.
Every positional argument needs to match, if you start rearranging the order by naming the arguments, you must rearrange all the arguments following the one you named.
So this line of code:
b = Enum.TryParse<MyEnum>(inputString, out res, ignoreCase: true);
Tries to match out res
with ignoreCase
, and then you come along naming that parameter again, which trips the compiler up. Likely there is another error just lurking behind the first one, that out res
is not a match for ignoreCase
.
So if you want to "skip" ignoreCase when dealing with positional arguments, you must name the out res
argument as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With