Search C # (Function 2) C # Talent Bird (QQ: 249178521)
4. Reference type parameters
· The reference type parameter is an alias in the argument
W No copy
W argument must be pre-assigned
W argument must be a variable type
W arguments and function parameters must have Ref
Sealed Class ParameterPassing
{
Static Void Method (Ref Int Parameter)
{
Parameter = 42;
}
Static void
Main
()
{
INT Arg = 0;
Console.write (arg); / / result is 0
Method (Ref Arg);
Console.write (arg); / / result is 42
}
}
The function parameter is referred to as a reference parameter when the REF modifier is called. The reference parameters do not produce new storage intervals. In fact, the reference parameter is the alias of the variable represented by the actual parameters transmitted when the function call is called. The result is another name of the reference parameter is only the variable represented by the argument.
The REF modifier must appear in the function declaration statement and function call statement.
Only those pre-assigned arguments can be passed to the reference type parameters, for example:
Int arg; // arg has not been assigned
Method (Ref arg); / / error, the first value must be pre-assigned
Communication to reference parameters must be variable types, not pure values or constants.
Method (Ref 42); // error, the reference parameter is not a pure value
Const int Arg = 42;
Method (Ref Arg); / / Error, the argument of reference parameters cannot be constant
5.out type parameters
· OUT type parameters are an alias in the argument
W No copy
W becene doesn't have to be pre-assigned
W argument must be a variable type
W function parameters must be pre-assigned to use
We must have OUT in W-Part-Parts and Functions.
Sealed Class ParameterPassing
{
Static Void Method (Out Int Parameter)
{
Parameter = 42;
}
Static void
Main
()
{
Int arg;
//CONSOLE.WRITE (ARG);
Method (Out Arg);
Console.write (arg); / / result is 42
}
}
When the function parameter has an OUT modifier, it is called an OUT type parameter. The OUT type parameter does not produce a new storage section. In fact, the OUT type parameter is an alias of the variable represented by the actual parameters transmitted when the function call is called. As a result, the OUT type parameter is only another name of the variable represented by the argument.
The OUT modifier must appear in the function declaration statement and function call statement.
Unit parameters that are not pre-assigned can be passed to reference type parameters, for example:
Int arg; // arg has not been assigned
Method (out arg); / / correct, informally
Communication to the OUT type parameter must be a variable type, not a pure value or constant.
Method (OUT 42); // Error, the argument of the OUT type parameter cannot be a pure value
Const int Arg = 42;
Method (out arg); // error, the unusual parameter of the OUT type can not be constant
6.in type parameter?
· Readonly, Const and IN, are C # keywords
W They cannot be used for function parameters
W Ref / OUT type parameters are always empowered
7. Function overload
· The function in a class can have the same name, called overload
The W function name and parameter are called the identity W identity must be unique.
W Return value type is not an identifier
Namespace System
{
Public Sealed Class Console
{
Public static void writeline ()
{...}
Public Static Void WriteLine (int value)
{...}
Public Static Void WriteLine (Double Value)
{...}
...
Public Static Void WriteLine (Object Value)
{...}
...
}
}
Like C , C # allows a class declared two or more symbol functions, as long as the type or number of parameters is different. This is overloaded. However, a class cannot contain an identifier as the same instance function and a static function, for example:
Sealed Class Illegal
{
Void overload () {...}
Static void overload () {...} // error
}
Like C , the type of return value is not part of the identity, and cannot be used as a standard of overload, for example:
Sealed Class Alsoillegal
{
Int random () {...}
Double Random () {...} // error
}