傳遞引用類型參數

引用類型的變量不直接包含其數據;它包含的是對其數據的引用。當經過值傳遞引用類型的參數時,有可能更改引用所指向的數據,如某類成員的值。可是沒法更改引用自己的值;也就是說,不能使用相同的引用爲新類分配內存並使之在塊外保持。若要這樣作,應使用 refout 關鍵字傳遞參數。爲了簡單起見,下面的示例使用 ref數組

下面的示例演示經過值向 Change 方法傳遞引用類型的參數 arr。因爲該參數是對 arr 的引用,因此有可能更改數組元素的值。可是,試圖將參數從新分配到不一樣的內存位置時,該操做僅在方法內有效,並不影響原始變量 arrapp

C#
 
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]);
    }
}

Inside Main, before calling the method, the first element is: 1ide

Inside the method, the first element is: -3spa

Inside Main, after calling the method, the first element is: 888code

在上個示例中,數組 arr 爲引用類型,在未使用 ref 參數的狀況下傳遞給方法。在此狀況下,將向方法傳遞指向 arr 的引用的一個副本。輸出顯示方法有可能更改數組元素的內容,在這種狀況下,從 1 改成 888。可是,在 Change 方法內使用 new 運算符來分配新的內存部分,將使變量 pArray 引用新的數組。所以,這以後的任何更改都不會影響原始數組 arr(它是在 Main 內建立的)。實際上,本示例中建立了兩個數組,一個在 Main 內,一個在 Change 方法內。ip

本示例除在方法頭和調用中使用 ref 關鍵字之外,其他與上個示例相同。方法內發生的任何更改都會影響調用程序中的原始變量。內存

C#
 
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]);
    }
}

Inside Main, before calling the method, the first element is: 1element

Inside the method, the first element is: -3字符串

Inside Main, after calling the method, the first element is: -3get

方法內發生的全部更改都影響 Main 中的原始數組。實際上,使用 new 運算符對原始數組進行了從新分配。所以,調用 Change 方法後,對 arr 的任何引用都將指向 Change 方法中建立的五個元素的數組。

交換字符串是經過引用傳遞引用類型參數的很好的示例。本示例中,str1str2 兩個字符串在 Main 中初始化,並做爲由 ref 關鍵字修改的參數傳遞給 SwapStrings 方法。這兩個字符串在該方法內以及 Main 內均進行交換。

C#
 
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);
    }
}

Inside Main, before swapping: John Smith

Inside the method: Smith John

Inside Main, after swapping: Smith John

本示例中,須要經過引用傳遞參數以影響調用程序中的變量。若是同時從方法頭和方法調用中移除 ref 關鍵字,則調用程序中不會發生任何更改。

相關文章
相關標籤/搜索