I. Learning Content
Construct various details of the C # method;
Out, ref, and Params keywords;
. Net 4 introduces new features: optional parameters and naming parameters.
2. Parameter modifiers of methods
1. No Modifier
If a method parameter is not marked with a parameter modifier, it will be passed by value, which means that the called method will get a copy of it.
This is the most common and common parameter tag form.
2. Out Modifier
When an out parameter is used to modify a parameter, this parameter is equivalent to the output parameter of this method;
It is passed by reference rather than by value;
The called method must be assigned a value for this parameter; otherwise, an error is reported during compilation;
When calling a method with the out parameter, the out modifier must also be used;
When you call a method with the out parameter, you can assign a value to the parameter first, because the called method will assign a value to it;
Its most important application is that multiple values can be returned in a method.
3. Ref Modifier
This is a method for passing values by reference ';
If you want the parameter passed to the called method to change the value in the method instead of a copy, you can declare the parameter as REF;
This parameter must have been assigned a value before calling the method, which is different from the out modifier;
4. Params Modifier
Pass the variable number of parameters (these parameters should be of the same type) as a single logical parameter to the method, similar to an array;
In the called method, Params parameters should be treated as an array, and zero input parameters are supported;
A Method supports up to one Params parameter, and must be the last parameter in the parameter list;
This technology facilitates callers.
Iii. New Features of. Net 4
1. Optional parameters
That is, only a parameter has specified a default value in the method declaration;
The value of the optional parameter must be determined at compilation, but cannot be determined at runtime; otherwise, an error is returned during compilation;
If an optional parameter is placed before a non-optional parameter, an error is reported during compilation.
2. Named Parameters
Parameter values can be specified in any order when a method is called;
During the call process, the named parameters must be placed at the end of the method call;
Optional parameters are often used with named parameters;
Optional and named parameters are supported mainly to simplify the interoperability with COM.
Member overloading:
Make sure that each method has a different parameter group: The number or type is different, but the members of different return types are not unique enough to meet the conditions.