ref和out

1.ref:

http://msdn.microsoft.com/zh-cn/library/14akc2c7.aspx

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 不同,後者的參數在傳遞之前不需要顯式初始化。

按引用傳遞值類型(如本主題前面所示)是有用的,但是 ref 對於傳遞引用類型也是很有用的。 這允許被調用的方法修改該引用所引用的對象,因爲引用本身是按引用來傳遞的。 下面的示例顯示出當引用類型作爲 ref 參數傳遞時,可以更改對象本身。

class RefExample2
{
    static void Method(ref string s)
    {
        s = "changed";
    }
    static void Main()
    {
        string str = "original";
        Method(ref str);
        Console.WriteLine(str);
    }
}
// Output: changed

2.out:

http://msdn.microsoft.com/zh-cn/library/ee332485.aspx

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 參數傳遞的變量不必在傳遞之前進行初始化,但被調用的方法需要在返回之前賦一個值。

當希望方法返回多個值時,聲明 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;
        }
    }
3.使用ref和out傳遞數組:

http://msdn.microsoft.com/zh-cn/library/szasx730(v=VS.90).aspx

在此例中,在調用方(Main 方法)中聲明數組 theArray,並在 FillArray 方法中初始化此數組。然後將數組元素返回調用方並顯示。

class TestOut
{
    static void FillArray(out int[] arr)
    {
        // Initialize the array:
        arr = new int[5] { 1, 2, 3, 4, 5 };
    }

    static void Main()
    {
        int[] theArray; // Initialization is not required

        // Pass the array to the callee using out:
        FillArray(out theArray);

        // Display the array elements:
        System.Console.WriteLine("Array elements are:");
        for (int i = 0; i < theArray.Length; i++)
        {
            System.Console.Write(theArray[i] + " ");
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
    /* Output:
        Array elements are:
        1 2 3 4 5       
    */

在此例中,在調用方(Main 方法)中初始化數組 theArray,並通過使用 ref 參數將其傳遞給 FillArray 方法。在 FillArray 方法中更新某些數組元素。然後將數組元素返回調用方並顯示。

class TestRef
{
    static void FillArray(ref int[] arr)
    {
        // Create the array on demand:
        if (arr == null)
        {
            arr = new int[10];
        }
        // Fill the array:
        arr[0] = 1111;
        arr[4] = 5555;
    }

    static void Main()
    {
        // Initialize the array:
        int[] theArray = { 1, 2, 3, 4, 5 };

        // Pass the array using ref:
        FillArray(ref theArray);

        // Display the updated array:
        System.Console.WriteLine("Array elements are:");
        for (int i = 0; i < theArray.Length; i++)
        {
            System.Console.Write(theArray[i] + " ");
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
    /* Output:
        Array elements are
        1111 2 3 4 5555
    */

發佈了27 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章