Method (function) in general, there will be incoming and outgoing arguments, and void type functions are no exception.
In general, a function returns a parameter through a return statement, and if more than one group (whether a single variable or set parameter) is returned, an incoming ref parameter is used. The following is a general summary of the contents of the function.
First, use optional parameters
In order to use optional parameters, you can provide a default value for the parameter when you define the method, as follows:
void Optmethod (int-A, double second=0.0, String third= "Hello");
In the above function, the one is a required parameter, and the second and third parameters are optional. The default parameter has to the left, which means that all optional parameters must be followed by the required arguments. You can call this function as follows:
Optmethod (99,123.45, "World");
Optmethod (99,123.45);
Optmethod ("World"); Can you? also requested proof
Second, pass named parameter
C # defaults to the parameter position to determine the formal parameters corresponding to the argument, as above. Now, C # also allows you to specify parameters by name, which enables you to pass the arguments in a different order. In order to pass an argument as a named argument,You must enter a parameter name, a colon, and then the value to be passed。 As follows:
Optmethod (First:99,second:123.45,third: "World");
Optmethod (first:99,second:123.45);
With named arguments, arguments can be passed in any order, as in the following rewrite code that calls the Optmethod method:
Optmethod (second:123.45,first:99);
You can also specify a section, leaving the optional parameter with the default value. As follows, second uses the default parameter
Optmethod (First:99,third: "World");
Iii. disambiguation of optional and named parameters
Here, give me two examples.
void Optmethod (int-A, double second=0.0,string third= "Hello");
void Optmethod (int-I, double second=1.0,string third= "GoodBye", int fourth=10);
When we call like this:
Optmethod (1,2.5, "World");
We simply don't know which function to call, in fact, it is. In fact, the call is the version that best matches the method, so the last option is to get 3-parameter versions instead of four.
Four, variable quantity parameter--parameter Array
4.1: declaring params array
The params keyword can be used as a modifier of an array parameter. The function is to pass any number of parameters. For example:
public static int Min (params int[] paramlist)
{}
1 if you want to determine which two integer value is large, you can call as follows: int min=min (First,second);
2 If you want to determine which three integer value is large, you can call as follows: int min=min (First,second,third);
If you do not define an array of params parameters, then write two parameters of different methods, that is, by overloading to achieve, slightly trouble bar.
Note:
1 only use the params keyword for one-dimensional arrays
2 cannot rely on keyword params to overload a method that does not form part of the method signature, as in the following example, compile-time error (duplicate definition):
public static int Min (int [] paramslist)
public static int Min (params int[] paramlist)
3 is not allowed to specify a ref or out modifier for a params array
4) The params array must be the last parameter of the formal parameter list, which also shows that there can be at most one params parameter in a function's formal parameter
5) A params method is always superior to the params method, that is, if you prefer, you can still create an overloaded version of a method to apply more general conditions, as follows:
public static int Min (int lefthandside,int righthandside)
public static int Min (params int[] params)
Note the difference between "so-called" overloads in and 2.
6 The compiler will reject ambiguous overloads. For example, the following two min methods are ambiguous, if you pass two int parameters, you will not be able to distinguish the specific should call which, compile-time error:
public static int Min (params int[] params)
public static int Min (Int,params int[] params)
4.2: Using params object[] array parameters
A parameter array of type int in 4.1 is useful because it allows any number of int parameters to be passed in a method call. However, if the number of parameters is not fixed, the parameter types are not fixed, and what to do. C # also provides a technique to resolve, that is, params object[] array parameters, which are defined as follows:
public static int Min (params object[] paramlist)
{}
It has the following characteristics:
1 can not pass any arguments to it, in which case the compiler passes an object array of length 0
2 can pass NULL as an argument because the array is a reference type, so null is allowed to initialize the array
3 can pass an actual array to the method
4 can pass any other parameter of different type to this method
Reference Parameters--reference parameters can be used to return function variables.
Typically, when a parameter is passed to a method, the corresponding parameter (formal parameter) is initialized with a secondary of the argument. Whether the argument is a value type (such as int), a nullable type (such as int?). or reference type (such as Wrappedint), which is true, in other words, any modification of a formal parameter within a method does not affect the original value of a variable passed as a parameter.
4.1:ref
If a parameter (formal parameter) attaches a ref keyword as a prefix, the argument becomes an alias (or a reference to the argument) of the argument and is no longer a copy of the argument. When you use the ref parameter, any action on the parameter applies equally to the argument, because the argument and the formal parameter refer to the same object.
4.2:out
The ref parameter must be assigned before it is used. However, sometimes we want to initialize the parameters by the method itself, so we want to pass an uninitialized parameter to it. The Out keyword is designed for this requirement.
The difference between 4.3:ref and out
The two are very much alike, and you can also make the formal parameter an alias to an argument, not a copy, except that:
1 out is the abbreviation for output, after passing an out parameter to the method, it must be assigned within its method, otherwise the code cannot be compiled.
static void doinitialize (out int param)
{
param=42;
}
static void Main ()
{
int arg;
Dointialize (out Arg);
Console.WriteLine (ARG); If there is no initialization param in doinitalize, compile error, otherwise display 42
}
2 The ref parameter must be initialized before it can be used, and the out parameter is not necessary. That is, the so-called ref has to enter, out of the only out.