static void Main(string[] args) { Program p = new Program(); int x = 100; int y = 200; //int x; //int y; p.GetValue(ref x, ref y); Console.WriteLine("x={0},y={1}",x,y);//新 的xy 没有覆盖掉原来的xy Console.ReadKey(); //int i = 500; //int c = 600; int i; int c; p.GetValue1(out i,out c); Console.WriteLine("i={0},c={1}", i, c); Console.ReadKey(); } public void GetValue(ref int x,ref int y) { x = 210; y = 220; } public void GetValue1(out int i,out int c) { i = 200; c = 404; }
ref :使用前必须赋值初始化,但是初始化值不一定使用,若方法中没有第一次初始值,则使用,若有则不使用。
out:使用前则不需要,但是初始值必须是方法中第一次初始值,也只能在方法中赋值初始化。