3,Params
ParamsYou can specify the parameter when the number of parameters is variable.
- In the method declarationParamsNo other parameters are allowed after the keyword, and only one parameter is allowed in the method declaration.
ParamsKeyword.
The literal meaning is difficult to understand, so it is useful to look at the example.
Using system;
Class app
{
Public static voiduseparams (Params object [] list)
{
For (INT I = 0; I <list. length; I ++)
{
Console. writeline (list [I]);
}
}
Static void main ()
{
//
The general practice is to first construct an object array and then use this array as a parameter of the method.
Object [] arr = newobject [3] {100, 'A', "keywords "};
Useparams (ARR );
//
After using the Params modifier parameters, we can directly use a group of objects as parameters.
// Of course, this set of parameters must meet the parameter requirements of the called Method
Useparams (100, 'A', "keywords ");
Console. Read ();
}
}
Keywords: ref and out
- To use the ref parameter, both the method definition and the call method must be explicitly used.RefKeyword.
- Parameters passed to the ref parameter must be initialized first. Unlike out, out parameters do not need to be explicitly initialized before being passed.
// Keywords_ref.cs
Using system;
Class app
{
Public static void useref (ref int I)
{
I + = 100;
Console. writeline ("I = {0}", I );
}
Static void main ()
{
Int I = 10;
// View the value before calling the Method
Console. writeline ("before themethod calling: I = {0}", I );
Useref (Ref I );
// View the value after the method is called
Console. writeline ("after themethod calling: I = {0}", I );
Console. Read ();
}
}
/*
Console output:
Beforethe method calling: I = 10
I = 110.
Afterthe method calling: I = 110
*/