A method is a member used to perform computing or operations that can be performed by objects or classes.
Static methods are accessed through classes.
The instance method is accessed through an instance of the class.
The method has a parameter list (which can be empty), indicating the value or variable reference passed to the method;
The return type specifies the type of the value to be calculated and returned by the method.
If the method does not return a value, the return type is void.
Similar to the type, a method can also have a set of type parameters. When calling a method, you must specify a type parameter for the type parameter.
Different from the type, the type real parameters can often be inferred from the real parameters of method calls without explicitly specifying them.
The method signature must be unique in the class that declares the method.
A Method signature consists of the method name, the number of type parameters, the number, modifier, and type of parameters of the method. The method signature does not contain the return type.
A parameter is used to pass a value or variable reference to a method.
The parameters of a method obtain their actual values from the real parameters (argument) specified when the method is called.
There are four types of parameters: Value parameter, reference parameter, output parameter, and parameter array.
Value parameter is used to pass input parameters.
A value parameter is equivalent to a local variable, but its initial value comes from the real parameter passed for this parameter.
Modifying a value parameter does not affect the real parameter passed by the parameter.
Value parameters can be optional. by specifying the default value, the corresponding real parameters can be omitted.
Reference parameter is used to pass input and output parameters.
The real parameter passed for the reference parameter must be a variable. During method execution, the reference parameter and the real parameter variable indicate the same storage location.
Reference parameters are declared using the ref modifier.
The following example demonstrates the ref parameter usage.
Using system;
Class test
{
Static void swap (ref int x, ref int y)
{
Int temp = x;
X = y;
Y = temp;
}
Static void main ()
{
Int I = 1, j = 2;
Swap (ref I, ref j );
Console. writeline ("{0} {1}", I, j); // outputs "2 1"
}
}
The output parameter is used to pass the output parameter.
For output parameters, the initial values of the real parameters provided by the caller are not important.
In addition, the output parameters are similar to the reference parameters.
Output parameters are declared with the out modifier.
The following example demonstrates the usage of the out parameter.
Using system;
Class test
{
Static void divide (int x, int y, out int result, out int remainder)
{
Result = x/y;
Remainder = x % y;
}
Static void main ()
{
Int res, rem;
Divide (10, 3, out res, out rem );
Console. writeline ("{0} {1}", res, rem); // outputs "3 1"
}
}
Parameter array allows passing a variable number of real parameters to the method.
The parameter array is declared using the params modifier.
Only the last parameter of the method can be a parameter array, and the parameter array type must be a one-dimensional array type.
The write and writeline methods of the system. console class are good examples of parameter array usage. Their declaration is as follows.
Public class console
{
Public static void write (string fmt, params object [] args ){...}
Public static void writeline (string fmt, params object [] args ){...}
} In the method of using parameter arrays, the behavior of parameter arrays is exactly like that of conventional array parameters.
However, in a method with a parameter array, you can either pass a single real parameter of the parameter array type or pass any number of real parameters of the element type of the parameter array.
In the latter case, an array instance is automatically created and initialized using the given real parameters. Example:
Console. writeline ("x = {0} y = {1} z = {2}", x, y, z); equivalent to the following statement:
String s = "x = {0} y = {1} z = {2 }";
Object [] args = new object [3];
Args [0] = x;
Args [1] = y;
Args [2] = z;
Console. writeline (s, args );