SoFunction
Updated on 2025-04-06

In-depth understanding of Ref, Out and its use


    class Program 
       { 
//After using out, the variable must be assigned
           public void TestOut(out int x, out int y) 
           { 
               x = 1; 
               y = 2; 
           } 
//The values ​​passed in at this time are x1:10, y1:11, and the values ​​of x1 after output are 2.

           public void TestRef(ref int x, ref int y) 
           { 
//The quote from the sentence "Cutting" is a pig, and the one that comes out may be a cow (very insightful!)
               x = 2; 

           } 
           static void Main(string[] args) 
           { 
               int x; 
               int y; 
               Program P1 = new Program(); 
               (out x,out y); 
               ("x={0},y={1}", x, y); 
//The ref must assign a value to the variable before use.
               int x1 = 10; 
               int Y1 = 11; 
               (ref x1,ref Y1); 
               ("x1={0},y1={1}", x1, Y1); 
           } 
       }