Method-the "secret" between REF and OUT

To return multiple values ​​of different types, use OUT to pass or REF to pass.

OUT: Initial value assigned in the method, only outgoing, used to return multiple values

The word out means "out", so adding out to the parameter means to pass the value assigned in the method to the outside. Assigning or not assigning outside the method does not work.

So out can only pass the initial value assigned in the method, but not the value assigned outside.

REF: When using REF, you must assign an initial value to this variable.

class Program
{
    static void Main(string[] args)
    {
        int number = 90;
        int num = Show(ref number);
        Console.WriteLine(number);
        Console.ReadKey();

    }

    public static int Show(ref int num) 
    {
         num = 10;
         return num + 10;
    }        
}

Note: The parameters in the method need to be modified with ref, and the method should also be modified with ref when calling this method. The same is true for out

The difference between REF and OUT:

Out can pass the value in the method, ref can pass the value in the method, and can pass the value out of the method.

Guess you like

Origin blog.csdn.net/TGB_Tom/article/details/108904058