问题
Sorry, if duplicate.
I'm reading CLR via C#. Chapter "Parameters" starts with the explanation of optional and named parameters.
So, can you give some example where using of named parameters has some benefits, or is it just the matter of style or habit? Do you personally use named parameters?
回答1:
Named parameters are very useful when combined with optional parameters in C# 4. This allows you to avoid providing a lot of method overloads, and instead just have a single one.
For example, instead of having 5 versions of a method, you can provide one method with multiple optional parameters, then call it as:
this.Foo("required argument", bar: 42);
This can simplify an API (one method instead of many), and still provide the same flexibility, without requiring the user to type in every argument. Without this, you'd either need many overloads, or have to provide all of the default values.
回答2:
Besides being used with optional parameters, named parameters are sometimes useful to make code more readable.
For example:
DrawLine(10, 10, 25, 16);
If you're not familiar with this (fictional) DrawLine method, but you know the box needs to be a little taller, you'd have to look up the method to determine which parameter to change. (Is it "Left, Top, Right, Bottom" or "Top, Left, Bottom, Right" or "Top, Height, Left, Width" etc.)
But:
DrawLine(left: 10, top: 10, width: 25, height: 16);
This makes clear exactly what is intended and which parameter to adjust.
回答3:
Suppose you have 5 optional boolean parameters, and you only want to pass in one of them.
Using named parameters lets you do that without any ambiguities.
回答4:
Named parameters is very much related with default values for parameters (optional) such as:
void Foo(int i = 10)
And is very good for several purposes as other answers have described already.
In my opinion (and I guess it is why we have had to rely on overloaded constructors until now) is to cut corners. In many cases some constructor can be more efficient if only a subset of another constructor is specified. Ex:
//Without optional parameters
public MyObj()
{
this.result = 3628800; //Factorial 10 - pretty fast
}
public MyObj(int n)
{
this.result = factorial(n);
}
//With optional parameters but same results for any number of n
public MyObj(int n = 10)
{
this.result = factorial(n); //A lot slower when we just need the result for 10
}
//Usage
new MyObj(); //Fast with constructor overloading - slower with optional parameters
This is of cause a stupid example but the idea is very important. I know Anders H. (co-author of C#) had this as an argument for not introducing optional parameters. But I guess most programmers will use the appropriate method when performance, readability etc. is emphasized in a project :)
It should also be noted that the newer initializer...
var foo = new Foo(...)
{
SomeProperty1 = something,
SomeProperty2 = something
}
...can be an good alternative to the named parameter style in many cases.
回答5:
I had a need of them a few days ago ... I had to give a full object parameter by parameter to a function and those were like 20 paramters, so i used named parameters for better understanding.
来源:https://stackoverflow.com/questions/3389649/usage-of-named-parameters