Keywords ref and out in C#

ref

ref: MSDN is defined as: "The ref keyword indicates a value that is passed by reference." is to pass parameters by reference. ref is also an abbreviation for Reference.

Do not use ref

using System;

namespace Test_Code
{
   class A
    {
       public void Method( int a)
       {
            a += 1;
       }

        public static void Main()
        {
            int c = 10;
            A B = new A();
            B.Method(c);
            Console.WriteLine(c);
        }
    }
}
//输出10

Use ref keywords

using System;

namespace Test_Code
{
    class A
    {
        public void Method(ref int a)
        {
            a += 1;
        }

        public static void Main()
        {
            int c = 10;
            A B = new A();
            B.Method(ref c);
            Console.WriteLine(c);
        }
    }
}
//输出11

总结

​ When the ref keyword is not used, the value received by the function is 10, and then in the Method (int a) method, after the local variable a is accumulated, it is destroyed after the method is executed. The value of c is still 10.
Using the ref keyword, the function Method (ref int a) received the value of c is the address of the function performed a + = 66; this case corresponds to c + = 66; directly modify the address value c.
So ref by a method of passing a value type parameter, a variable direct operation of the same key.

out

Definition of out: As a parameter modifier, which lets you pass an argument to a method by reference rather than by value. "out" as a parameter modifier allows you to pass parameters to a method by reference rather than by value.
​ In generic type parameter declarations for interfaces and delegates, which specifies that a type parameter is covariant./In generic type parameter declarations for interfaces and delegates, it specifies that type parameters are covariant. In today's context, we will only discuss the first definition of passing parameters as a reference.

int number;
 
Method(number);
 
void Method(int a)
{
    a = 66;
}
 
Console.WriteLine(number);
//输出:0
 
 
 
int number;
 
Method(out number);
 
void Method(out int a)
{
    a = 66;
}
 
Console.WriteLine(number);
//输出:66

总结

Judging from the performance of the above out usage, both out and ref can actually allow parameters to be passed by reference.

The difference between ref and out:

​ When you use ref to pass parameters, the ref modified parameter must have a value, but out can use an unassigned variable as a parameter to pass.

using System;

namespace Test_Code
{
    class RefAndOut
    {
        public static void OutDouble(out int out_int)
        {
            out_int = 2;
            Console.WriteLine(out_int);
        }
        public static void RefDouble(ref int ref_int)
        {
            ref_int *= 2;
            Console.WriteLine(ref_int);
            Console.ReadKey();
        }
        public static void NormalDouble(int IntPar)
        {
            IntPar = 1;
            IntPar *= 2;
            Console.WriteLine( IntPar);
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            int out_int;
            int ref_int;
            int normalInt;
            OutDouble(out out_int);
            RefDouble(ref ref_int);//错误:使用了未赋值的变量refInt
            NormalDouble(normalInt);//错误:使用了未赋值的变量normalInt
        }
    }

}

This code has errors in two places: that is, when ref is used, and when modifiers are not used, a parameter with a value must be passed. So you see, there is almost only one difference between ref and out, that is, out can use unassigned variables.

in conclusion:

​ The only difference between the keywords "ref" and "out" is that the keyword "out" does not require the calling code to initialize the parameter value to be passed. So when is the keyword "ref" used? When you need to ensure that the calling method has initialized parameter values, you should use the keyword "ref". In the above example, the ability to use "out" is because of the method being called It does not depend on the value of the variable being passed.

Guess you like

Origin blog.csdn.net/qq_40513792/article/details/115168287