In depth understanding and use of Ref and Out

  • 2020-05-12 03:14:02
  • OfStack


    class Program  
       {  
           // use out You must then assign a value to the variable   
           public void TestOut(out int x, out int y)  
           {  
               x = 1;  
               y = 2;  
           }  
           // The values passed in are x1:10,y1:11, After output x1 The value of 2  

           public void TestRef(ref int x, ref int y)  
           {  
               // It was the pig that quoted the scissors , It could be a cow ( Very incisive! )  
               x = 2;  

           }  
           static void Main(string[] args)  
           {  
               int x;  
               int y;  
               Program P1 = new Program();  
               P1.TestOut(out x,out y);  
               Console.WriteLine("x={0},y={1}", x, y);  
               // Before use ref A variable must be assigned a value   
               int x1 = 10;  
               int Y1 = 11;  
               P1.TestRef(ref x1,ref Y1);  
               Console.WriteLine("x1={0},y1={1}", x1, Y1);  
           }  
       }  

Related articles: