傳遞參考型別的參數 (C# 程式設計手冊)

參考型別的參數並不會直接包含它的資料;它包含資料的參考。 以傳值方式傳遞參考型別參數時,它有可能會變更到參考指到的資料,例如類別成員的值。 然而,您無法變更參考本身的值;您不能使用相同的參考來對一個新類別配置記憶體和使它保存在區塊外。 若要完成上面所述,請使用 refout 關鍵字傳遞參數。 為了簡化,下列範例使用 ref。

範例

下列範例說明以傳值方式將參考型別參數 arr 傳遞到 Change 方法。 因為參數是 arr 的參考,它有可能變更到陣列元素的值。 然而,嘗試將參數指派到不同的記憶體位置,只會在方法內產生作用,並不會影響到原本的變數 arr。

class PassingRefByVal 
{
    static void Change(int[] pArray)
    {
        pArray[0] = 888;  // This change affects the original element.
        pArray = new int[5] {-3, -1, -2, -3, -4};   // This change is local.
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
    }

    static void Main() 
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);

        Change(arr);
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);
    }
}
/* Output:
    Inside Main, before calling the method, the first element is: 1
    Inside the method, the first element is: -3
    Inside Main, after calling the method, the first element is: 888
*/

在之前的範例中,參考具型別陣列 arr 被傳遞到方法中沒有使用 ref 參數。 在這個例子,傳遞到方法的是指向 arr 參考的複本。 輸出顯示,此種方法有可能變更陣列元素的內容,在此情況下為從 1 到 888。 然而,在 Change 方法內使用 new 運算子配置新的記憶體部分,即可使變數 pArray 參考產生新的陣列。 因此,之後的任何變更並不會影響到在 Main 中所產生的原始陣列 arr。 事實上,本範例中建立了兩個陣列:一個在 Main 裡面,另一個在 Change 方法內。

本範例除了在方法標頭和呼叫使用 ref 關鍵字外,其他部分與上一個範例相同。 在該方法內的任何變更都會影響進行呼叫之程式內的原始變數。

class PassingRefByRef 
{
    static void Change(ref int[] pArray)
    {
        // Both of the following changes will affect the original variables:
        pArray[0] = 888;
        pArray = new int[5] {-3, -1, -2, -3, -4};
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
    }

    static void Main() 
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);

        Change(ref arr);
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);
    }
}
/* Output:
    Inside Main, before calling the method, the first element is: 1
    Inside the method, the first element is: -3
    Inside Main, after calling the method, the first element is: -3
*/

發生在方法內的所有變更會影響 Main 裡面的原始陣列。 事實上,原始陣列是用 new 運算子重新配置的。 因此,呼叫 Change 方法後,任一個 arr 的參考都會指到 Change 方法內所建立五個元素的陣列。

以傳址方式傳遞參考型別參數的良好範例便是交換字串。 在本範例中,字串 str1 和 str2 是在 Main 內初始化,並以 ref 關鍵字修改它來做為傳遞到 SwapStrings 方法的參數。 兩個字串同時在方法內和 Main 內交換。

 class SwappingStrings
 {
     static void SwapStrings(ref string s1, ref string s2)
     // The string parameter is passed by reference.
     // Any changes on parameters will affect the original variables.
     {
         string temp = s1;
         s1 = s2;
         s2 = temp;
         System.Console.WriteLine("Inside the method: {0} {1}", s1, s2);
     }

     static void Main()
     {
         string str1 = "John";
         string str2 = "Smith";
         System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);

         SwapStrings(ref str1, ref str2);   // Passing strings by reference
         System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
     }
 }
 /* Output:
     Inside Main, before swapping: John Smith
     Inside the method: Smith John
     Inside Main, after swapping: Smith John
*/

在本範例中,參數需要以傳址方式傳遞參數以影響呼叫程式內的變數。 如果您將 ref 關鍵字從方法標頭和方法呼叫中移除,則在呼叫程式中不會發生任何變更。

如需字串的詳細資訊,請參閱字串

請參閱

參考

傳遞參數 (C# 程式設計手冊)

使用 ref 和 out 傳遞陣列 (C# 程式設計手冊)

ref (C# 參考)

參考型別 (C# 參考)

概念

C# 程式設計手冊