c#中通過值和引用傳遞參數(downmoon)
在 C# 中,既可以通過值也可以通過引用傳遞參數。通過引用傳遞參數允許函數成員(方法、屬性、索引器、運算符和構造函數)更改參數的值,并保持該更改。若要通過引用傳遞參數,請使用 ref 或 out 關鍵字。為簡單起見,本主題的示例中隻使用了 ref 關鍵字。有關 ref 和 out 之間的差異的資訊,請參見、使用 ref 和 out 傳遞數組。
本主題包括下列章節:
- 傳遞值類型參數
- 傳遞引用類型參數
它還包括以下示例:
示例 | 示範 | 是否使用 ref 或 out |
---|---|---|
1 | 通過值傳遞值類型 | 否 |
2 | 通過引用傳遞值類型 | 是 |
3 | 交換值類型(兩個整數) | 是 |
4 | 通過值傳遞引用類型 | 否 |
5 | 通過引用傳遞引用類型 | 是 |
6 | 交換引用類型(兩個字元串) | 是 |
傳遞值類型參數
值類型變量直接包含其資料,這與引用類型變量不同,後者包含對其資料的引用。是以,向方法傳遞值類型變量意味着向方法傳遞變量的一個副本。方法内發生的對參數的更改對該變量中存儲的原始資料無任何影響。如果希望所調用的方法更改參數值,必須使用 ref 或 out 關鍵字通過引用傳遞該參數。為了簡單起見,以下示例使用 ref。
示例 1:通過值傳遞值類型
下面的示例示範通過值傳遞值類型參數。通過值将變量
myInt
傳遞給方法
SquareIt
。方法内發生的任何更改對變量的原始值無任何影響。
// PassingParams1.cs
using System;
class PassingValByVal
{
static void SquareIt(int x)
// The parameter x is passed by value.
// Changes to x will not affect the original value of myInt.
{
x *= x;
Console.WriteLine("The value inside the method: ", x);
}
public static void Main()
{
int myInt = 5;
Console.WriteLine("The value before calling the method: ",
myInt);
SquareIt(myInt); // Passing myInt by value.
Console.WriteLine("The value after calling the method: ",
myInt);
}
}
輸出
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”相同。參數的值在調用方法後發生更改。
// PassingParams2.cs
using System;
class PassingValByRef
{
static void SquareIt(ref int x)
// The parameter x is passed by reference.
// Changes to x will affect the original value of myInt.
{
x *= x;
Console.WriteLine("The value inside the method: ", x);
}
public static void Main()
{
int myInt = 5;
Console.WriteLine("The value before calling the method: ",
myInt);
SquareIt(ref myInt); // Passing myInt by reference.
Console.WriteLine("The value after calling the method: ",
myInt);
}
}
輸出
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
。
// PassingParams4.cs
// Passing an array to a method without the ref keyword.
// Compare the results to those of Example 5.
using System;
class PassingRefByVal
{
static void Change(int[] arr)
{
arr[0]=888; // This change affects the original element.
arr = new int[5] {-3, -1, -2, -3, -4}; // This change is local.
Console.WriteLine("Inside the method, the first element is: ", arr[0]);
}
public static void Main()
{
int[] myArray = ;
Console.WriteLine("Inside Main, before calling the method, the first element is: ", myArray [0]);
Change(myArray);
Console.WriteLine("Inside Main, after calling the method, the first element is: ", myArray [0]);
}
}
輸出
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”相同。方法内發生的任何更改都會影響調用程式中的原始變量。
// PassingParams5.cs
// Passing an array to a method with the ref keyword.
// Compare the results to those of Example 4.
using System;
class PassingRefByRef
{
static void Change(ref int[] arr)
{
// Both of the following changes will affect the original variables:
arr[0]=888;
arr = new int[5] {-3, -1, -2, -3, -4};
Console.WriteLine("Inside the method, the first element is: ", arr[0]);
}
public static void Main()
{
int[] myArray = ;
Console.WriteLine("Inside Main, before calling the method, the first element is: ", myArray [0]);
Change(ref myArray);
Console.WriteLine("Inside Main, after calling the method, the first element is: ", myArray [0]);
}
}
輸出
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
方法中建立的五個元素的數組。
示例 6:交換兩個字元串
交換字元串是通過引用傳遞引用類型參數的很好的示例。本示例中,
str1
和
str2
兩個字元串在 Main 中初始化,并作為由 ref 關鍵字修飾的參數傳遞給
SwapStrings
方法。這兩個字元串在該方法内以及 Main 内均進行交換。
// PassingParams6.cs
using System;
class SwappinStrings
{
static void SwapStrings(ref string s1, ref string s2)
// The string parameter x is passed by reference.
// Any changes on parameters will affect the original variables.
{
string temp = s1;
s1 = s2;
s2 = temp;
Console.WriteLine("Inside the method: , ", s1, s2);
}
public static void Main()
{
string str1 = "John";
string str2 = "Smith";
Console.WriteLine("Inside Main, before swapping: ",
str1, str2);
SwapStrings(ref str1, ref str2); // Passing strings by reference
Console.WriteLine("Inside Main, after swapping: , ",
str1, str2);
}
}
輸出
Inside Main, before swapping: John Smith
Inside the method: Smith, John
Inside Main, after swapping: Smith, John
代碼讨論
本示例中,需要通過引用傳遞參數以影響調用程式中的變量。如果同時從方法頭和方法調用中移除 ref 關鍵字,則調用程式中不會發生任何更改。