Named parameters and optional parameters in C,
The named parameters and optional parameters are new in C # framework 4.0.
1. Definitions and calls of common methods
Public void Demo1 (string x, int y) {// do something...} public void Main () {// call Demo1 ("similar", 22 );}
The Parameter order (type) must be consistent with the declaration and cannot be omitted.
2. Declaration and call of optional parameters
Optional parameters are divided into two types: 1. Some parameters are optional; 2. All parameters are optional.
// Partially optional (required for x and optional for y) public void Demo2 (string x, int y = 5) {// do something ...} public void Main () {// call Demo2 ("similar"); // y uses the default value of 5 Demo2 ("similar", 10) if no real parameter is input ); // input the real parameter by y. Then, the real parameter 10 is used}
Note: When the parameter is partially optional, the declaration of the optional parameter must be defined after the non-optional parameter (The Declaration of the preceding: y is after x). Otherwise, the following error message is displayed:
// All optional (x and y are optional parameters) public void Demo3 (string x = "demo", int y = 5) {// do something ...} public void Main () {// call Demo3 (); // If x and y do not input real parameters, the default values of x and y are "demo" and 5 Demo3 ("similar "); // if y does not pass in real parameters, y uses the default value 5 Demo3 ("similar", 10); // x, y all input real parameters}
Sub-order.
B. The parameter Declaration definition can be unordered, but must be consistent with the Declaration.
The above call only writes three types. In fact, there is another one, that is, x uses the default value, and y passes in the real parameter, that is, Demo3 (10 );
However, an error is returned when this call is performed because the first parameter of Demo3 is of the string type and the error message is returned.
But now I only want to input y and do not want to input x. What should I do? I need to use the named parameter C.
Iii. Named Parameters
The use of the named parameter is mainly reflected in the function call.
Public void Main () {// call Demo3 (); // If x and y do not input real parameters, the default values of x and y are "demo" and 5 Demo3 ("similar "); // when y does not pass in real parameters, y uses the default value 5 Demo3 ("similar", 10); // x, y all input real parameters // use Demo3 (y: 10 );}
Using the named parameter, we can specify the value of a specific parameter. Here we can solve the problem we encountered through Demo3 (y: 10) (x uses the default value, and y uses the real parameter ).
Note: When you use a named parameter, you do not need to worry about the declared order of the parameter when calling the method. That is, the following call method is also possible:
When you call a method that contains optional parameters, a smart prompt is displayed in vs, prompting you which parameters are optional and Their default values. brackets indicate optional []:
Provided by "figure douluo"