在C#中关于传递参数ref和out分别有什么区别

2013-05-25ref(C# 参考)
      ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。例如:
class RefExample {     static void Method(ref int i)     {         i = 44;     }     static void Main()     {         int val = 0;         Method(ref val);         // val is now 44     } }
    传递到 ref 参数的参数必须最先初始化。这与 out 不同,out 的参数在传递之前不需要显式初始化。(请参见 out。)
    尽管 refout 在运行时的处理方式不同,但它们在编译时的处理方式是相同的。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:
class CS0663_Example  {     // compiler error CS0663: "cannot define overloaded      // methods that differ only on ref and out"     public void SampleMethod(ref int i) {  }     public void SampleMethod(out int i) {  } }
    但是,如果一个方法采用 refout 参数,而另一个方法不采用这两类参数,则可以进行重载,如下所示:
class RefOutOverloadExample {     public void SampleMethod(int i) {  }     public void SampleMethod(ref int i) {  } }
备注      属性不是变量,因此不能作为 ref 参数传递。
    有关传递数组的信息,请参见使用 refout 传递数组。
示例 
    按引用传递值类型(如上所示)是有用的,但是 ref 对于传递引用类型也是很有用的。这允许被调用的方法修改该引用所引用的对象,因为引用本身是按引用来传递的。下面的示例显示出当引用类型作为 ref 参数传递时,可以更改对象本身。
class RefRefExample {     static void Method(ref string s)     {         s = "changed";     }
    static void Main()     {         string str = "original";         Method(ref str);         // str is now "changed"     } }
out(C# 参考)
    out 关键字会导致参数通过引用来传递。这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化。若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字。例如:
class OutExample {     static void Method(out int i)     {         i = 44;     }
    static void Main()     {         int value;         Method(out value);         // value is now 44     } }
   尽管作为 out 参数传递的变量不需要在传递之前进行初始化,但需要调用方法以便在方法返回之前赋值。
   refout 关键字在运行时的处理方式不同,但在编译时的处理方式相同。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:
class CS0663_Example  {     // compiler error CS0663: "cannot define overloaded      // methods that differ only on ref and out"     public void SampleMethod(out int i) {  }     public void SampleMethod(ref int i) {  } }
    但是,如果一个方法采用 refout 参数,而另一个方法不采用这两类参数,则可以进行重载,如下所示:
class RefOutOverloadExample {     public void SampleMethod(int i) {  }     public void SampleMethod(out int i) {  } }
备注      属性不是变量,因此不能作为 out 参数传递。
    有关传递数组的信息,请参见使用 refout 传递数组。
示例 
    当希望方法返回多个值时,声明 out 方法很有用。使用 out 参数的方法仍然可以将变量用作返回类型(请参见 return),但它还可以将一个或多个对象作为 out 参数返回给调用方法。此示例使用 out 在一个方法调用中返回三个变量。请注意,第三个参数所赋的值为 Null。这样便允许方法有选择地返回值。
class OutReturnExample {     static void Method(out int i, out string s1, out string s2)     {         i = 44;         s1 = "I've been returned";         s2 = null;     }
    static void Main()     {         int value;         string str1, str2;         Method(out value, out str1, out str2);         // value is now 44         // str1 is now "I've been returned"         // str2 is (still) null;     } }
ref(C# 参考)

      ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。例如:

class RefExample
{
    static void Method(ref int i)
    {
        i = 44;
    }
    static void Main()
    {
        int val = 0;
        Method(ref val);
        // val is now 44
    }
}

    传递到 ref 参数的参数必须最先初始化。这与 out 不同,out 的参数在传递之前不需要显式初始化。(请参见 out。)

    尽管 refout 在运行时的处理方式不同,但它们在编译时的处理方式是相同的。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:

class CS0663_Example 
{
    // compiler error CS0663: "cannot define overloaded 
    // methods that differ only on ref and out"
    public void SampleMethod(ref int i) {  }
    public void SampleMethod(out int i) {  }
}

    但是,如果一个方法采用 refout 参数,而另一个方法不采用这两类参数,则可以进行重载,如下所示:

class RefOutOverloadExample
{
    public void SampleMethod(int i) {  }
    public void SampleMethod(ref int i) {  }
}

备注 
    属性不是变量,因此不能作为 ref 参数传递。

    有关传递数组的信息,请参见使用 refout 传递数组。

示例 

    按引用传递值类型(如上所示)是有用的,但是 ref 对于传递引用类型也是很有用的。这允许被调用的方法修改该引用所引用的对象,因为引用本身是按引用来传递的。下面的示例显示出当引用类型作为 ref 参数传递时,可以更改对象本身。

class RefRefExample
{
    static void Method(ref string s)
    {
        s = "changed";
    }

    static void Main()
    {
        string str = "original";
        Method(ref str);
        // str is now "changed"
    }
}

out(C# 参考)

    out 关键字会导致参数通过引用来传递。这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化。若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字。例如:

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }

    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

   尽管作为 out 参数传递的变量不需要在传递之前进行初始化,但需要调用方法以便在方法返回之前赋值。

   refout 关键字在运行时的处理方式不同,但在编译时的处理方式相同。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:

class CS0663_Example 
{
    // compiler error CS0663: "cannot define overloaded 
    // methods that differ only on ref and out"
    public void SampleMethod(out int i) {  }
    public void SampleMethod(ref int i) {  }
}

    但是,如果一个方法采用 refout 参数,而另一个方法不采用这两类参数,则可以进行重载,如下所示:

class RefOutOverloadExample
{
    public void SampleMethod(int i) {  }
    public void SampleMethod(out int i) {  }
}

备注 
    属性不是变量,因此不能作为 out 参数传递。

    有关传递数组的信息,请参见使用 refout 传递数组。

示例 

    当希望方法返回多个值时,声明 out 方法很有用。使用 out 参数的方法仍然可以将变量用作返回类型(请参见 return),但它还可以将一个或多个对象作为 out 参数返回给调用方法。此示例使用 out 在一个方法调用中返回三个变量。请注意,第三个参数所赋的值为 Null。这样便允许方法有选择地返回值。

class OutReturnExample
{
    static void Method(out int i, out string s1, out string s2)
    {
        i = 44;
        s1 = "I've been returned";
        s2 = null;
    }

    static void Main()
    {
        int value;
        string str1, str2;
        Method(out value, out str1, out str2);
        // value is now 44
        // str1 is now "I've been returned"
        // str2 is (still) null;
    }
}
原文地址:https://www.cnblogs.com/ruishuang208/p/3099028.html