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: {0}", x); } public static void main() { int myint = 5; console.writeline("the value before calling the method: {0}", myint); squareit(myint); // passing myint by value. console.writeline("the value after calling the method: {0}", myint); }}輸出
the value before calling the method: 5the value inside the method: 25the 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: {0}", x); } public static void main() { int myint = 5; console.writeline("the value before calling the method: {0}", myint); squareit(ref myint); // passing myint by reference. console.writeline("the value after calling the method: {0}", myint); }}輸出
the value before calling the method: 5the value inside the method: 25the 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: {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]); }}輸出
inside main, before calling the method, the first element is: 1inside the method, the first element is: -3inside 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: {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]); }}輸出
inside main, before calling the method, the first element is: 1inside the method, the first element is: -3inside main, after calling the method, the first element is: -3代碼討論
方法內發生的所有更改都影響 main 中的原始數組。實際上,使用 new 運算符對原始數組進行了重新分配。因此,調用 change 方法后,對 myarray 的任何引用都將指向 change 方法中創建的五個元素的數組。
示例 6:交換兩個字符串
交換字符串是通過引用傳遞引用類型參數的很好的示例。本示例中,str1 和 str2 兩個字符串在 main 中初始化,并作為由 ref 關鍵字修飾的參數傳遞給 swapstrings 方法。這兩個字符串在該方法內以及 main 內均進行交換。
// passingparams6.csusing 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: {0}, {1}", s1, s2); } public static void main() { string str1 = "john"; string str2 = "smith"; console.writeline("inside main, before swapping: {0} {1}", str1, str2); swapstrings(ref str1, ref str2); // passing strings by reference console.writeline("inside main, after swapping: {0}, {1}", str1, str2); }}輸出
inside main, before swapping: john smithinside the method: smith, johninside main, after swapping: smith, john代碼討論
本示例中,需要通過引用傳遞參數以影響調用程序中的變量。如果同時從方法頭和方法調用中移除 ref 關鍵字,則調用程序中不會發生任何更改。
注冊會員,創建你的web開發資料庫,
新聞熱點
疑難解答