C#的引用參數和值參數
ref 關鍵字使參數按引用傳遞。其效果是,當控製權傳遞回調用方法時,在方法中對參數所做的任何更改都將反映在該變量中。
2.1. 若要使用 ref 參數,則方法定義和調用方法都必須顯式使用 ref 關鍵字。
2.2. 傳遞到 ref 參數的參數必須最先初始化。這與 out 不同,out 的參數在傳遞之前不需要顯式初始化。
2.3. 如果一個方法采用 ref 或 out 參數,而另一個方法不采用這兩類參數,則可以進行重載。
示例 1:通過值傳遞值類型
下麵的示例演示通過值傳遞值類型參數。通過值將變量 myInt 傳遞給方法 SquareIt。方法內發生的任何更改對變量的原始值無任何影響。
class PassingValByVal { static void SquareIt(int x) { x *= x; Console.WriteLine("The valueinside the method: {0}", x); } public static void Main() { int myInt = 5; Console.WriteLine("The valuebefore calling the method: {0}", myInt); SquareIt(myInt); // Passing myInt byvalue. Console.WriteLine("The valueafter calling the method: {0}", myInt); Console.ReadKey(); } }
輸出
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 5
代碼討論
變量 myInt 為值類型,包含其數據(值 5)。當調用 SquareIt時,myInt
的內容被複製到參數 x中,在方法內將該參數求平方。但在 Main
中,myInt的值在調用 SquareIt
方法之前和之後是相同的。實際上,方法內發生的更改隻影響局部變量 x。
示例 2:通過引用傳遞值類型
下麵的示例除使用 ref 關鍵字傳遞參數以外,其餘與“示例 1”相同。參數的值在調用方法後發生更改。
class PassingValByRef { static void SquareIt(ref int x) { x *= x; Console.WriteLine("The valueinside the method: {0}", x); } public static void Main() { int myInt = 5; Console.WriteLine("The valuebefore calling the method: {0}", myInt); SquareIt(ref myInt); Console.WriteLine("The valueafter calling the method: {0}", myInt); Console.ReadKey(); } }
輸出
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 25
代碼討論
本示例中,傳遞的不是myInt 的值,而是對 myInt的引用。參數 x
不是 int類型,它是對 int
的引用(本例中為對 myInt的引用)。因此,當在方法內對 x
求平方時,實際被求平方的是 x所引用的項:myInt。
示例 3:交換值類型
更改所傳遞參數的值的常見示例是 Swap 方法,在該方法中傳遞 x和 y
兩個變量,然後使方法交換它們的內容。必須通過引用向 Swap方法傳遞參數;否則,方法內所處理的將是參數的本地副本。以下是使用引用參數的 Swap方法的示例:
static void SwapByRef(ref int x, ref int y) { int temp = x; x = y; y = temp; }調用該方法時,請在調用中使用ref 關鍵字,如下所示:
SwapByRef (ref i, ref j);
傳遞引用類型參數
引用類型的變量不直接包含其數據;它包含的是對其數據的引用。當通過值傳遞引用類型的參數時,有可能更改引用所指向的數據,如某類成員的值。但是無法更改引用本身的值;也就是說,不能使用相同的引用為新類分配內存並使之在塊外保持。若要這樣做,請使用 ref(或 out)關鍵字傳遞參數。為了簡單起見,以下示例使用 ref。
示例 4:通過值傳遞引用類型
下麵的示例演示通過值向Change 方法傳遞引用類型的參數myArray。由於該參數是對myArray的引用,所以有可能更改數組元素的值。但是,試圖將參數重新分配到不同的內存位置時,該操作僅在方法內有效,並不影響原始變量
myArray。
class PassingRefByVal { static void Change(int[] arr) { arr[0] = 888; arr = new int[5] { -3, -1, -2, -3,-4 }; Console.WriteLine("Inside themethod, the first element is: {0}", arr[0]); } public static void Main() { int[] myArray = { 1, 4, 5 }; Console.WriteLine("Inside Main,before calling the method, the first element is: {0}", myArray[0]); Change(myArray); Console.WriteLine("Inside Main,after calling the method, the first element is: {0}", myArray[0]); Console.ReadKey(); } }
輸出
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
代碼討論
在上個示例中,數組myArray為引用類型,在未使用 ref參數的情況下傳遞給方法。在此情況下,將向方法傳遞指向 myArray的引用的一個副本。輸出顯示方法有可能更改數組元素的內容(從
1改為 888)。但是,在 Change方法內使用
new運算符分配新的內存部分,將使變量 arr引用新的數組。因此,這之後的任何更改都不會影響原始數組 myArray(它是在
Main內創建的)。實際上,本示例中創建了兩個數組,一個在 Main內,一個在 Change方法內。
示例 5:通過引用傳遞引用類型
本示例除在方法頭和調用中使用ref 關鍵字以外,其餘與“示例 4”相同。方法內發生的任何更改都會影響調用程序中的原始變量。
class PassingRefByRef { static void Change(ref int[] arr) { // Both of the following changeswill affect the original variables: arr[0] = 888; arr = new int[5] { -3, -1, -2, -3,-4 }; Console.WriteLine("Inside themethod, the first element is: {0}", arr[0]); } public static void Main() { int[] myArray = { 1, 4, 5 }; Console.WriteLine("Inside Main,before calling the method, the first element is: {0}", myArray[0]); Change(ref myArray); Console.WriteLine("Inside Main,after calling the method, the first element is: {0}", myArray[0]); Console.ReadKey(); } }
輸出
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 方法後,對 myArray的任何引用都將指向 Change 方法中創建的五個元素的數組。
3. 輸出類型(out類型)
out 關鍵字會導致參數通過引用來傳遞。這與 ref關鍵字類似。
與 ref 的不同之處:
3.1. ref 要求變量必須在傳遞之前進行初始化,out 參數傳遞的變量不需要在傳遞之前進行初始化。
3.2. 盡管作為 out 參數傳遞的變量不需要在傳遞之前進行初始化,但需要在調用方法初始化以便在方法返回之前賦值。
最後更新:2017-04-03 12:54:13