C#: The connection and difference between ref and out

One: The ref keyword causes parameters to be passed by reference.
The effect is that any changes made to parameters in the method will be reflected in that variable when control is passed back to the calling method. To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword.
That is to say, the setting and changing of parameters in the method will directly affect the place where the function is called (codes ① and ②). The keyword ref cannot be ignored either in the definition of the function or in the call.
You can compare the code:
Code ①:
copy code
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Program pg = new Program();
 6             int x = 10;
 7             int y = 20;
 8             pg.GetValue(ref x, ref  y);
 9             Console.WriteLine("x={0},y={1}", x, y);
10 
11             Console.ReadLine();
12           
13         }
14 
15         public void GetValue(ref int x, ref int y)
16         {
17             x = 521;
18             y = 520;
19         }
20     }
copy code

 

The result of running is 

Code ② :

copy code
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Program pg = new Program();
 6             int x = 10;
 7             int y = 20;
 8             pg.GetValue(ref x, ref  y);
 9             Console.WriteLine("x={0},y={1}", x, y);
10 
11             Console.ReadLine();
12           
13         }
14 
15         public void GetValue(ref int x, ref int y)
16         {
17             x = 1000;
18             y = 1;
19         }
20     }
copy code

 

由代码① 和②的运行结果可以看出,在方法中对参数所做的任何更改都将反映在该变量中,而在main函数中对参数的赋值却没有起到作用,这是不是说明不需要进行初始化呢?来看第二点

二:ref定义的参数在使用前必须初始化,无论在函数定义时候参数有没有赋予初值。这条正好区分out指定的参数可以不在调用函数的时候进行初始化。
来看代码③ 以及其运行结果:
copy code
 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Program pg = new Program();
 6             int x;
 7             int y;        //此处x,y没有进行初始化,则编译不通过。
 8             pg.GetValue(ref x, ref  y);
 9             Console.WriteLine("x={0},y={1}", x, y);
10 
11             Console.ReadLine();
12           
13         }
14 
15         public void GetValue(ref int x, ref int y)
16         {
17             x = 1000;
18             y = 1;
19         }    
20     }
copy code

 

出现的错误为:使用了未赋值的局部变量“x”,“y”。故可以说明ref指定的参数无论在函数定义的时候有没有赋予初值,在使用的时候必须初始化。

三 :对out来说,第一条同样适用。将代码①以及②中的ref全部修改成out,则可与使用ref得到同样的结果。
 
四:out指定的参数必须在函数定义的时候就赋初值。否则则出现错误。对比ref指定的参数则可以不在函数内部进行赋初值,在函数调用时候再赋初值也可以。
代码④:
copy code
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Program pg = new Program();
 6             int x=10;
 7             int y=233;
 8             pg.Swap(out x, out y);
 9             Console.WriteLine("x={0},y={1}", x, y);
10 
11             Console.ReadLine();
12           
13         }
 14  
15          public  void Swap( out  int a, out   int b)
 16          {
 17              
18              int temp = a;    // a, b have no initial value inside the function, an error occurs. 
19              a = b;
 20              b = temp;
 21          }     
 22      
23      }
copy code

 

An error occurred: Unassigned out parameters "a", "b" are used
Then when the swap function is defined, assign initial values ​​to a and b to run correctly.
 
Summarizing the above four, the difference between the use of ref and out is:
①: The parameter specified by ref must be initialized when the function is called, and cannot be an empty reference. The parameters specified by out may not be initialized when the function is called;
②: The parameter specified by out will clear itself when entering the function, and an initial value must be assigned inside the function. The parameters specified by ref are not required.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326993412&siteId=291194637