I. Method
1. What is a method?
The method is to reuse a piece of code.
2. method definition:
[Access modifier] [static] Method Name of Return Value Type ()
{
Method body;
}
Note: [] is optional.
3. Pay attention to the following details:
Naming rules: method names start with uppercase, parameter names start with lowercase, and parameter names and variable names must be meaningful.
4. method parameters:
1> defining variables in the brackets behind the method name is called defining the parameters (parameters) of this method ).
2> when multiple parameters are defined in the method (), they are separated by commas (,). No matter whether the types of parameters are the same, they cannot be defined as multiple variables of the same type: for example, int A, B, C, D; can only define parameters
A data type is followed by a variable name. Because the parameter type can be different, it is certainly not supported to define multiple variables of the same type ).
3> the method parameter can have the default value. If the method parameter has the default value, we can skip this parameter when calling this method. If this parameter is not set, the default value is used,
If a parameter is input, the value of the parameter is the value of the real parameter.
4> if some parameters of a method have default values, while others do not, the parameters with default values must appear on the rightmost side of the parameter list. If there are parameters with default values in the method, we can have different numbers when passing parameters,
However, the type and position of the parameter to be assigned must correspond to each other.
5> the displayed value is the form parameter of the method. Syntax: Method Name (form parameter name: real parameter ). You can assign values to parameters by using the display method instead of by order. However, the number and type must correspond.
6> Params is a key word. It can only be used to modify parameters of a method. The parameter to be modified can only be a one-dimensional array, and none of other parameters can be used.
A. When assigning values to the parameter (one-dimensional array) modified by Params, you can assign a one-dimensional array, but you can also directly assign values to the element. Each value is separated by a comma. Because the C # compiler will encapsulate the data into an array during compilation
(The length of the array is determined by the caller.) In fact, the parameter still has only one array, and it is also transmitted as an array.
B. parameters modified by Params must be placed at the end of the parameter list. because the length of the one-dimensional array modified by Params is unknown, the parameter compiler cannot identify which parameters are modified by Params or other parameters.
So it can only be placed at the end.
C. Parameters modified by Params can only appear once in the parameter list.
D. When passing the parameter modified for Params, one or not can be passed because the array length can be 0;
E. Determine whether the element value is transmitted by determining whether the array length is 0 within the method.
F. If an overload is formed, the method without Params will be matched first during the call. If Params is not matched, the method with Params will be matched again.
Ii. Return Value
A. Why is there a return value?
Because when the caller needs to use parameters in the method, we need the method to return the value of the parameter to us.
1> return is a keyword used to return the return value of a method and terminate the current method (the method of return) immediately. Syntax: return value;
2> ref is a key word and can only be used to modify parameters of a method (the implication is that variables cannot be modified by REF). Any parameters can be modified by Ref.
1> If the method parameter includes ref, the real parameter must also contain ref.
2> when we assign values to the shape parameters modified by REF, it is not the passed real parameter value, but the memory address of the real variable. This is why the value of the local variable modified inside the method also changes.
Because what we actually change is the value of the external real parameter.
3> when we pass a value for the ref parameter, we must initialize the real parameter variable before passing the value.
4> when passing a value for a ref parameter, you must give one variable but not one constant, because ref passes the address of the variable.
5> parameters modified by REF cannot contain default values. The variable address is required for ref, but the real parameter is not the variable address.
3> out is a key word and can only be used to modify parameters of a method (the implication is that variables cannot be modified by OUT). Any parameters can be modified by out.
1> If the method parameter has an out parameter, the real parameter must also contain an out parameter.
2> when we assign a value to the out parameter, it is not the passed real parameter value, but the memory address of the real parameter variable. This is why the value of the local variable modified inside the method also changes.
Because what we actually change is the value of the external real parameter.
3> parameters modified by the out parameter can be assigned no value outside the method, but must be assigned a value within the method, and must be assigned before use.
4> when passing a value for an out parameter, you must give one variable but not one constant, because out is the address of the variable.
5>. Because the out parameter requires the variable address to be passed, the out parameter cannot contain the default value.
4> similarities and differences between ref and out:
1> ref must have a value before passing real parameters. But out can have no value.
2> The out value must be assigned internally and before use, while the ref value can be assigned internally.
3> conclusion: the ref parameter must be assigned a value outside, while the out parameter must be assigned a value before being used internally. Both ref and out are passed variable addresses, and they are modified values of real parameters.
Summary return values:
With return, we can return a data or a data set. However, this requires that the data type must be the same or compatible. Therefore, return is generally used for a set with only one return value or the same data type.
Ref and out return values through parameters. They are generally used to return multiple values of different data types.
3. Call Methods
1> In the same namespace, the static method can be called by class name. Method Name (parameter.
2> all methods without static modification in the same namespace must use the new keyword to create a real column and call it with the real column name. Method Name (parameter.
3> If the namespace is not in the same namespace, you must first import the namespace, and then create an object's real column to call the method. Static Method: class name. Method Name
Iv. Method Overloading
A. What is method overload?
Generally, in the same class (or parent class), the method name is the same, and the number of parameters of the method is different, or the parameter type at the corresponding position is different.
B. the overload of a method has no relationship with the return value, but only with parameters (the premise method name is the same)
V. Scope of Variables
1. Local variables:
A. What is a local variable?
The variables defined in the method are called local variables. The scope of the variables starts from the definition and ends with the braces.