SoFunction
Updated on 2025-03-01

Detailed explanation of parameter problems based on params, ref, out

Recently, I encountered params, ref, out parameters problems when writing programs. I went back to study and looked at MSDN to consolidate my foundation. Let me share it with you now.
params
The params keyword is used in the parameter list of method members, providing the method with the ability to variable number of parameters. It can only appear once and cannot have parameter definitions after it (it can be done before).
Example:

Copy the codeThe code is as follows:

using System;
using ;
using ;
namespace ConsoleApplication1
{
class App
{
//The first parameter must be an integer, but the number of subsequent parameters is variable.
//And since the object array is set, all data types can be passed in as parameters
public static void UseParams(int id, params object[] list)
{
(id);
for (int i = 0; i < ; i++)
{
(list[i]);
}
}
static void Main()
{
//The variable parameter part has three parameters passed in, all of which are string types
UseParams(1, "a", "b", "c");
//The variable parameter part has four parameters passed in, namely string, integer, floating point number and double precision floating point number array, namely string, integer, floating point number and double precision floating point number array
UseParams(2, "d", 100, 33.33, new double[] {1.1, 2.2});
();
}
}
}

ref
The ref keyword causes the parameters to be passed by reference. The effect is that when control is passed back to the call method, any changes made to the parameters in the method will be reflected in the variable.
1. To use the ref parameter, both the method definition and the method calling must use the ref keyword explicitly.
2. The parameters passed to the ref parameter must be initialized first. This is different from out, where the parameters of out do not need to be explicitly initialized before being passed.
3. Properties are not variables, so they cannot be passed as ref parameters.
4. Although ref and out are handled differently at runtime, they are handled the same at compile time. Therefore, if one method takes the ref parameter and the other method takes the out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are exactly the same. If you try to do this, the code will not be compiled.
5. If one method takes ref or out parameters, and the other method does not take these two types of parameters, 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.
Copy the codeThe code is as follows:

using System;
class App
{
    public static void UseRef(ref int i)
    {
        i += 100;
        ("i = {0}", i);
    }
    static void Main()
    {
        int i = 10;
// Check the value before calling the method
        ("Before the method calling: i = {0}", i);
        UseRef(ref i);
// Check the value after calling the method
        ("After the method calling: i = {0}", i);
        ();
     }
}

out
The out keyword causes the parameters to be passed by reference. This is similar to the ref keyword.
What's different from ref:
It is required that the variable must be initialized before being passed.
2. Although variables passed as out parameter do not need to be initialized before passing, methods need to be called to assign values ​​before the method returns.
Example:
Different from the ref example, just change ref to out, and then the variable i only needs to be declared.
Copy the codeThe code is as follows:

static void Main()
{
//int i = 10; Change to
    int i;
}