傳遞參考型別參數(C# 編程指南)

來源:互聯網
上載者:User

參考型別的變數不直接包含其資料;它包含的是對其資料的引用。 當通過值傳遞參考型別的參數時,有可能更改引用所指向的資料,如某類成員的值。 但是無法更改引用本身的值;也就是說,不能使用相同的引用為新類分配記憶體並使之在塊外保持。 若要這樣做,應使用 ref 或 out 關鍵字傳遞參數。 為了簡單起見,下面的樣本使用 ref。

樣本

下面的樣本示範通過值向 Change 方法傳遞參考型別的參數 arr。 由於該參數是對 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 引用新的數組。 因此,這之後的任何更改都不會影響原始數組 arr(它是在 Main 內建立的)。 實際上,本樣本中建立了兩個數組,一個在 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 關鍵字,則調用程式中不會發生任何更改。 
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.