ref和out

 
       共同点:方法参数上的 ref 和out方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
       不同点:传递到 ref 参数的变量必须最先初始化。传递到 out 参数的变量不必初始化,然而,必须在方法返回之前为 out 参数赋值。
        注意:无法定义仅在 ref 和 out 方面不同的重载,以下重载声明是无效的:
 class MyClass
 { 
        public void MyMethod(out int i) {i = 10;}
        public void MyMethod(ref int i) {i = 10;}
}
当希望方法返回多个值时,声明 out 方法非常有用。
示例
// cs_out.cs
 using System;
 public class MyClass
 {
     public static int TestOut(out char i,out char j)
     {
         i = 'b';
         j = 'c'; 
        return -1;
     }
     public static void Main()
     {
         char i; // variable need not be initialized char j; 
        Console.WriteLine(TestOut(out i,out j)); 
        Console.WriteLine(i);
         Console.WriteLine(j);
     }
}
原文地址:https://www.cnblogs.com/zhangzheny/p/615551.html