The named parameter and optional parameter are new features of the C # Framework 4.0.
I. General method definition and invocation
public void Demo1 (string x, int y) { //do something ...} public void Main () { //Call Demo1 ("similar", 22);}
When called, the parameter order (type) must be consistent with the Declaration and cannot be omitted.
Two. Declaration and invocation of optional parameters
The optional parameters are divided into two cases: 1. Some parameters are optional; 2. All parameters are optional
Partial optional (x is required, y is optional) public void Demo2 (string x, int y = 5) { //do something ...} public void Main () { //Call Demo2 ("similar"); Y does not pass in the argument, Y uses the default value of 5 Demo2 ("Similar", "ten"); Y to pass in the argument, use argument 10}
Note: When a parameter is partially optional, the declaration of an optional parameter must be defined after the non-optional parameter (as above: Y is declared after x), otherwise the following error message appears:
All optional (x, y are optional parameters) public void Demo3 (String x = "Demo", int y = 5) { //do something ...} public void Main () { //Call Demo3 (); When x, y does not pass in the argument, X, Y uses the default value of "Demo", 5 Demo3 ("similar"); Y does not pass in the argument, Y uses the default value of 5 Demo3 ("Similar", "ten"); X, y both pass in the argument}
Note: A. When the parameters are all optional, the declaration order of the parameters can be arbitrarily defined, not in sequence.
B. parameter declaration definitions can be non-sequential, but must be invoked at the same time as the declaration.
The above call only write 3 kinds, in fact, there is another, that is, x using the default value, y incoming arguments, namely: Demo3 (10);
However, this call will error because the first parameter of DEMO3 is a string type, error message
But now I just want to pass in the Y, do not want to pass in X, what to do, then need to use C # named parameters.
Three. Named parameters
The use of named parameters is mainly reflected in the function call.
public void Main () { //Call Demo3 (); When x, y does not pass in the argument, X, Y uses the default value of "Demo", 5 Demo3 ("similar"); Y does not pass in the argument, Y uses the default value of 5 Demo3 ("Similar", "ten"); X, y both pass in the argument
Use Demo3 (y:10) for named parameters;}
With the named parameter, we can specify the value of a particular parameter, where we can solve the problem we have encountered with Demo3 (Y:10) (x uses the default value, Y uses the argument).
Note: When using a named parameter, the calling method can be used without the declaration order of the parameters, that is, the following method of invocation is also possible:
When calling a method with optional arguments, there are smart hints in VS that indicate which parameters are optional and their default values, and the brackets denote an optional []:
From
Similar named parameters and optional parameters in C #
Named and optional parameters in C #