Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing named arguments in C#

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.

like image 295
UserControl Avatar asked Jul 02 '12 20:07

UserControl


People also ask

How do you specify a named argument?

Named arguments The argument for each parameter can be specified by parameter name. For example, a function that prints order details (such as, seller name, order number & product name) can be called by sending arguments by position, in the order defined by the function.

Should Use named arguments?

Always use named arguments in constructor calls Constructor arguments are often unclear. There could be a lot of arguments, multiple arguments of the same type, default arguments or everything combined. In most cases, readability improves when using named arguments in constructor calls.

Why is it a good idea to use named parameters instead of the syntax?

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.

Does C++ support named parameters?

In Python I used named parameter(keyword argument) for function calls. Wikipedia page about named parameter tells that C++ doesn't support it.


2 Answers

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:

    int RegisterUser( #if DEBUG       int _ = 0, #endif       string nameFirst = null,       string nameLast = null,       string nameMiddle = null,       string email = null) { /*...*/ } 

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:

    RegisterUser();     RegisterUser(nameFirst: "Joe");     RegisterUser(nameFirst: "Joe", nameLast: "Smith");     RegisterUser(email: "[email protected]"); 

When attempting to use positional parameters, the code won't compile.

like image 123
user4698855 Avatar answered Sep 22 '22 13:09

user4698855


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).

like image 35
driis Avatar answered Sep 19 '22 13:09

driis