C # Reference method parameter keywords: params, ref, and out
If ref or out is not used when declaring a parameter for a method, the parameter can have an associated value. The value can be changed in the method, but the changed value is not preserved when control is passed back to the calling procedure. You can change this behavior by using the method parameter keyword.
params
The params keyword can specify a method parameter that takes a parameter at a variable number of arguments.
- No additional arguments are allowed after the params keyword in the method declaration, and only one params keyword is allowed in the method declaration.
Example :
The literal meaning is rather difficult to understand, so it is useful to look at examples.
Keywords_params.cs
Using System;
Class APP
{
public static void Useparams (params object[] list)
{
for (int i = 0; i < list. Length; i++)
{
Console.WriteLine (List[i]);
}
}
static void Main ()
{
It is common practice to construct an array of objects and then use this array as a parameter to the method
object[] arr = new Object[3] {A, ' a ', "keywords"};
Useparams (arr);
With the params modification method parameter, we can use a set of objects directly as parameters
Of course, this set of parameters needs to conform to the calling method of the parameter requirements
Useparams (b, ' a ', "keywords");
Console.read ();
}
}
ref
The ref keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.
- To use the ref parameter, both the method definition and the calling method must explicitly use the ref keyword.
- The arguments passed to the ref parameter must be initialized first. This differs from out in that the out parameter does not need to be explicitly initialized before it is passed.
- property is not a variable and therefore cannot be passed as a ref parameter.
- Although ref and out are handled differently at run time, they are handled identically at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical. Attempting to do so will result in the code not being compiled.
- If a method takes a ref or out parameter, and the other method does not take these two types of arguments, it can be overloaded.
Example :
Passing value types by reference is useful, but ref is also useful for passing reference types. This allows the called method to modify the object referenced by the reference, because the reference itself is passed by reference.
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 the method calling: i = {0}", i );
useref (ref i);
// view values after calling a method
console.writeline ("after the method calling: i = {0}", i);
console.read ();
}
}
/*
Console output:
before the method CALLING&NBSP;:&NBSP;I&NBSP;=&NBSP;10
i = 110
After the method calling: i = 110
*/
out
The Out keyword causes parameters to be passed by reference. This is similar to the REF keyword.
differs from ref:
- Ref requires that a variable be initialized before it is passed.
- Although a variable passed as an out parameter does not need to be initialized before it is passed, the method needs to be called to assign a value before the method returns.
Example :
As with the ref example, just change ref to out, and the variable I only needs to be declared.
static void Main ()
{
int i = 10; Switch
int i;
//
}
Category: C # basics
C # Reference method parameter keywords: params, ref, and out