C# 4 introduced a feature called named arguments which is especially useful in scenarios like
int RegisterUser(string nameFirst, string nameLast, string nameMiddle, string email)
Is there a way to force using named arguments? Maybe some attribute to apply to a method or a compiler switch I'm not aware of? I guess it can be done with code inspector tools but just want to know if there is other way.
p.s.
For those interested why one may need it and why not just use a class/struct to utilize object initializers there are scenarios when it's impossible. Like calls to libraries not in your control or weird code conventions you have to obey.
I'm using another method. In my setup I have 1 parameter which I always expect, then come a bunch of optional strings which I really want to be sure the user chose actively. So my first string in this list is a "trap" value, which if set, throws an error. Like this:
Hope you like it :)
I've also sought a way to force named arguments. Optional parameters can be dangerous, especially if you have multiple parameters of the same type. Overloads are almost always a safer solution, but there are times when you have a method that can take many combination of arguments, so creating 20 overloads to account for ever possibility is overkill.
In extreme situations where it is of the utmost importance that arguments be named at all times, I will create an argument class with no defined constructor. In your case, you could do this:
Call it like this:
You could also simplify it like this:
...and do this:
This way, you only have one optional parameter and that's for your optional parameters.
Edit: Maybe I didn't read the OP correctly. You're not using optional arguments? If not then this answer probably doesn't help you.
Sorry for a shameless plug!
I implemented a Roslyn analyzer to enforce using named arguments for a method.
So if you install the RequireNamedArgs analyzer and add a special comment before a method that should be invoked with named arguments:
The analyzer will emit an error if a caller attempts to use positional arguments instead of named.
Take a look at it in action:
If you decide to give it a go -- do so at your own risk :)
No, not in the C# language. It will always accept positional parameters if all the parameters are supplied.
You could build a custom FxCop rule or an StyleCop rule to enforce this - as pointed out in the comments, it is likely a StyleCop rule you would be interested in (thanks to Kris).
It's possible to force the callers to always use named args. I wouldn't do this in most circumstances because it's rather ugly, but it depends on how badly safe method usage is needed.
Here is the solution:
The first parameter is a dummy that shouldn't be used (and is compiled away in Release for efficiency). However, it ensures that all following parameters have to be named.
Valid usage is any combination of the named parameters:
When attempting to use positional parameters, the code won't compile.