Details of parameter problems based on params ref and out

  • 2020-05-17 06:12:14
  • OfStack

Recently, I encountered params, ref, out parameter problems while writing the program. Back to have a look at MSDN, to consolidate the foundation. Now let me share one with you.
params
The params keyword is used in a method member's argument list, giving the method the ability to have a variable number of arguments that can occur only once and cannot be defined later (previously).
Example:


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class App
{
// The first 1 The number of arguments must be integer, but the number of arguments is variable. 
// And because of the object Array, all data types can be passed in as parameters 
public static void UseParams(int id, params object[] list)
{
Console.WriteLine(id);
for (int i = 0; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
}
static void Main()
{
// The variable parameters section is passed in 3 Two parameters, all of which are string types 
UseParams(1, "a", "b", "c");
// The variable parameters section is passed in 4 Is a string, integer, floating point number and double precision floating point number group 
UseParams(2, "d", 100, 33.33, new double[] {1.1, 2.2});
Console.ReadLine();
}
}
}

ref
The ref keyword causes the parameter to be passed by reference. The effect is that when control is passed back to the calling method, any changes made to the parameters in the method are reflected in the variable.
1. To use the ref parameter, both the method definition and the method invocation must explicitly use the ref keyword.
2. 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 they are passed.
3. Properties are not variables, so they cannot be passed as ref parameters.
4. Although ref and out are handled differently at run time, they are handled the same at compile time. Therefore, if one method takes the ref parameter and the other 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 identical. If you try to do this, you will fail to compile the code.
5. If one method takes ref or out parameters and the other method does not, overload can be performed.
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 that the reference refers to, since the reference itself is passed by reference.

using System;
class App
{
    public static void UseRef(ref int i)
    {
        i += 100;
        Console.WriteLine("i = {0}", i);
    }
    static void Main()
    {
        int i = 10;
        //  Look at the value before calling the method 
        Console.WriteLine("Before the method calling: i = {0}", i);
        UseRef(ref i);
        //  View the value after the method is invoked 
        Console.WriteLine("After the method calling: i = {0}", i);
        Console.Read();
     }
}

out
The out keyword causes the parameter to be passed by reference. This is similar to the ref keyword.
Differences from ref:
1.ref requires that variables be initialized before they are passed.
2. Although variables passed as out arguments do not need to be initialized before they are passed, methods need to be called to assign values before the method returns.
Example:
Unlike the ref example, just change ref to out, and then the variable i only needs to be declared.

static void Main()
{
    //int i = 10;  Instead of 
    int i;
}


Related articles: