1. 程式人生 > >ref和out

ref和out

引用 ram 引用類型 brush temp string emp 之前 pro

  ref關鍵字:

    是用來修飾形參的,可以將值類型當做引用類型來使用

    ref修飾的形參,在方法內部修改的時候,會影響實參的值

    ref修飾的形參,在傳參的時候,實參的值可以帶入方法中

    ref修飾的形參,在方法結束之前可以不賦值

  out關鍵字:

    out關鍵字:

      用來修飾形參的,out修飾的形參必須在方法結束之前賦值

      out修飾的形參,在方法內部進行修改的時候,會影響實參的值

      out修飾的形參,在傳參的時候,實參的值不會帶入到方法中

      out可以變相的使用多返回

class Program
{
    static void Main(string[] args)
    {
        int m = 10, n, x, y;
        Add(m, out n, out x, out y);
        int i = 10, j = 20;
        Change(ref i, ref j);
    }
    static void Change(ref int a, ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    static void Add(int a,out int b,out int c,out int d)
    {
        b = 1;
        c = 2;
        d = 3;
    }
}

  

ref和out