Home > Article > Backend Development > A brief discussion on the six parameters of C# methods
C# method has six parameters, namely value parameters, reference parameters, output parameters, parameter arrays, named parameters, and optional parameters. The following article will introduce it to you. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Value parameter
The value parameter is the default type of the method, by copying the value of the actual parameter to The data is passed to the method in the form of formal parameters. When the method is called, the system performs two-step operations;
1. Allocate space for the formal parameters on the stack.
2. Copy the actual parameters to the formal parameters.
1) Before the method is called, the reference of variable a1 used as an actual parameter is already on the stack.
2) As the method begins, the system allocates space on the stack for the formal parameters and copies the values from the actual parameters.
3) Because a1 is a reference type, the reference is copied, and as a result, both the actual parameters and the formal parameters refer to the same object in the heap.
4) Because a2 is a value type, the value is copied, producing an independent data.
5) After the method is executed, the formal parameters are popped from the stack.
6) a2. Value type, its value is not affected by method behavior.
7) a1. Reference type, its value is changed by the behavior of the method.
Reference parameters
1. When using reference parameters, the ref modifier must be used in the declaration and call of the method.
2. The actual parameter must be a variable and must be assigned a value before being used as an actual parameter.
For value parameters, the system allocates memory for the formal parameter on the stack; conversely, for reference parameters, the parameter name of the formal parameter looks like an alias for the actual parameter variable , that is, they point to the same memory location. Therefore, any changes made to the formal parameters during the execution of the method are still valid (shown in the actual parameter variables) after the method execution is completed.
The transfer of reference parameters is completely transferred to both value types and reference types. There is no copying of values and references, that is, the same memory location is referenced.
Output parameters
Output parameters are used to pass data from the method body to the calling code. They are very similar to reference parameters.
1. The out modifier must be used in the declaration and call of the method.
2. Similar to reference parameters, actual parameters must be variables.
Just like reference parameters, the formal parameters of the output parameters serve as aliases for the actual parameters. Any changes to the formal parameters within the method will be visible through the actual parameter variables after the method execution is completed.
Different from reference parameters, output parameters have the following two requirements.
1. Inside the method, the output parameter must be assigned a value before being read, which means that the initial value of the parameter is irrelevant, so there is no need to assign a value to the actual parameter before the method is called.
2. Before the method returns, any possible path through the method must be assigned once to all output parameters.
Parameter array
In the above 3 parameter methods, one actual parameter must strictly correspond to one formal parameter. The parameter array is different. It allows Zero or more actual parameters correspond to a special formal parameter.
The requirements for parameter arrays are as follows:
1. There can only be one parameter array in a parameter list.
2. If there is, it must be the last one in the list.
Declaring a parameter array must have the params modifier (no modifier is required for the call), and it must be an array of one type.
When using distributed parameter passing, the compiler does the following:
1) Accepts the actual parameter list and uses them to create and initialize a array.
2) Save the reference of the array to the formal parameter on the stack.
3) If there is no actual parameter in the corresponding formal parameter group position, the compiler will create an array with zero elements for use.
4) If the array parameter is a value type, then the value is copied and the actual parameters are not affected inside the method.
5) If the array parameter is a reference type, then the reference is copied, and the object referenced by the actual parameter can be affected within the method.
When using array parameter passing, the compiler uses your data instead of recreating one. That is equivalent to reference parameters.
Named parameters
The four parameters used above are all positional parameters, which means that the position of each actual parameter must be the same. One corresponds to the corresponding formal parameter position.
Starting from .Net4.0, as long as the name of the specified parameter is displayed, the actual parameters can be listed in any order in the method call. The details are as follows.
1. There is no difference in the declaration of methods. The formal parameters already have names.
2. However, when calling a method, the name of the formal parameter is followed by a colon and the actual parameter value or expression.
When calling, positional parameters and named parameters can also be mixed, but all positional parameters must be listed first,
Optional parameters
The optional parameters were added only in .Net4.0. You can include this parameter when calling the method, or you can Omit it.
In order to indicate that a parameter is optional, you need to provide a default value for the parameter when the method is declared. The syntax for specifying default values is the same as for initializing local variables.
As shown below
1) The formal parameter b is set to the default value.
2) Therefore, when calling the method, there is only one parameter, and the method will use 3 as the initialization of the second parameter.
There are several important matters regarding the declaration of optional parameters:
1. Not all parameter types can be used as optional parameters.
1) As long as the default value of the value type can be determined at compile time, the value type can be used as an optional parameter.
2). Reference types can be used as optional parameters only when the default value is null.
3). Optional parameters can only be value parameters.
2. All required parameters must be declared before the optional parameters are declared. If there are params parameters, they must be declared after all optional parameters.
3. It must be omitted from the end of the optional parameter list to the beginning, otherwise it will cause parameter ambiguity.
4. If you want to eliminate parameter ambiguity, you can combine the features of named parameters and optional parameters.
As shown below
This article comes from the C#.Net Tutorial column, welcome to learn!
The above is the detailed content of A brief discussion on the six parameters of C# methods. For more information, please follow other related articles on the PHP Chinese website!