Need to know: the relationship between classes and methods
Method and Parameter modifiers
Custom methods can have or have no parameters, or they can have or have no return value. Can be modified by various keywords (static, virtual, public, new, etc.) to limit their behavior.
C # parameter modifiers
None If a parameter is not marked with a parameter modifier , it is considered to be passed by value , which means that the method being called receives a copy of the original data.
The out output parameter is assigned a value by the called method , so it is passed by reference. If the called method does not assign a value to the output parameter, a compiler error occurs.
The ref caller assigns an initial value and can be optionally re-assigned by the called Method . If the called method fails to assign a value to the ref parameter, there is no compiler error.
Params This parameter modifier allows a variable number of parameters to be passed as a separate logical parameter. Method can have only one params modifier , and must be the last parameter of the method.
Default parameter passing behavior
Reference type:
Regardless of whether a value type or a reference type is passed here, the values before and after the pass will not change. The ADD () method operates on a copy of the data and does not affect the data itself. It is like declaring a parameter in the method that accepts the passed argument, and in the method it is the newly declared parameter that is not the parameter we pass in.
Out modifier
The out modifier is called an output parameter. It is an obligation to define a method with an output parameter (through the keyword out) to assign an appropriate value to the parameter before exiting the method. Calling a method with an output parameter also requires an out modifier.
Rules:
① declares that a variable is not assigned (assignment compilation is not wrong)
② This variable into the method in out form
This variable has a value after the ③ method is called
Ref modifier
If you want a method to operate on different data declared in the caller's scope (usually changing its value), such as: sorting and exchanging routines, you need to use reference parameters.
output parameters do not need to be initialized before they are passed to the method , because the method must assign a value to the output parameter before exiting .
reference parameters must be initialized before they are passed to the method , because a reference to the existing variable is passed.
Ref Example:
Rules:
① Declaration of a variable (initialization)
② passing parameters in ref form
③ Method of operation
params modifier
C # supports the use of parameter arrays using the params keyword. The params keyword can pass a variable number of arguments (the same type) as a single logical parameter to the method.
Note : To avoid ambiguity (multiple definitions), C # requires the method to support only one params parameter , and must be the last parameter in the parameter list.
Defining Optional parameters
The optional parameter is assigned when the parameter is declared, the same as with an initial value, the caller can need to change this value as needed.
Note : The value of the optional parameter is determined rather than generated by the time of compilation. Optional parameters must be placed at the end of the method signature .
Calling methods using named arguments
Named parameters allow you to specify the value of a parameter in any order when the method is called. Therefore, you can use the colon operator to specify parameters by name .
The use of named parameters with optional parameters is more effective.
Method overloading
When a set of identically named methods is defined, the number of arguments (or types) is different, so the method is called overloaded.
For example: Calculate the number of two and this is the same as an overloaded
C # Foundation One (method detailed)