在 c# 中,既可以通過值也可以通過引用傳遞參數(shù)。通過引用傳遞參數(shù)允許函數(shù)成員(方法、屬性、索引器、運(yùn)算符和構(gòu)造函數(shù))更改參數(shù)的值,并保持該更改。若要通過引用傳遞參數(shù),請使用 ref 或 out 關(guān)鍵字。為簡單起見,本主題的示例中只使用了 ref 關(guān)鍵字。有關(guān) ref 和 out 之間的差異的信息,請參見、使用 ref 和 out 傳遞數(shù)組。
本主題包括下列章節(jié):
傳遞值類型參數(shù)
傳遞引用類型參數(shù)
它還包括以下示例:
示例 演示 是否使用 ref 或 out
1 通過值傳遞值類型 否
2 通過引用傳遞值類型 是
3 交換值類型(兩個(gè)整數(shù)) 是
4 通過值傳遞引用類型 否
5 通過引用傳遞引用類型 是
6 交換引用類型(兩個(gè)字符串) 是
傳遞值類型參數(shù)
值類型變量直接包含其數(shù)據(jù),這與引用類型變量不同,后者包含對其數(shù)據(jù)的引用。因此,向方法傳遞值類型變量意味著向方法傳遞變量的一個(gè)副本。方法內(nèi)發(fā)生的對參數(shù)的更改對該變量中存儲(chǔ)的原始數(shù)據(jù)無任何影響。如果希望所調(diào)用的方法更改參數(shù)值,必須使用 ref 或 out 關(guān)鍵字通過引用傳遞該參數(shù)。為了簡單起見,以下示例使用 ref。
示例 1:通過值傳遞值類型
下面的示例演示通過值傳遞值類型參數(shù)。通過值將變量 myint 傳遞給方法 squareit。方法內(nèi)發(fā)生的任何更改對變量的原始值無任何影響。
// 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 為值類型,包含其數(shù)據(jù)(值 5)。當(dāng)調(diào)用 squareit 時(shí),myint 的內(nèi)容被復(fù)制到參數(shù) x 中,在方法內(nèi)將該參數(shù)求平方。但在 main 中,myint 的值在調(diào)用 squareit 方法之前和之后是相同的。實(shí)際上,方法內(nèi)發(fā)生的更改只影響局部變量 x。
示例 2:通過引用傳遞值類型
下面的示例除使用 ref 關(guān)鍵字傳遞參數(shù)以外,其余與“示例 1”相同。參數(shù)的值在調(diào)用方法后發(fā)生更改。
// 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 的引用。參數(shù) x 不是 int 類型,它是對 int 的引用(本例中為對 myint 的引用)。因此,當(dāng)在方法內(nèi)對 x 求平方時(shí),實(shí)際被求平方的是 x 所引用的項(xiàng):myint。
示例 3:交換值類型
更改所傳遞參數(shù)的值的常見示例是 swap 方法,在該方法中傳遞 x 和 y 兩個(gè)變量,然后使方法交換它們的內(nèi)容。必須通過引用向 swap 方法傳遞參數(shù);否則,方法內(nèi)所處理的將是參數(shù)的本地副本。以下是使用引用參數(shù)的 swap 方法的示例:
static void swapbyref(ref int x, ref int y){ int temp = x; x = y; y = temp;}調(diào)用該方法時(shí),請?jiān)谡{(diào)用中使用 ref 關(guān)鍵字,如下所示:
swapbyref (ref i, ref j);傳遞引用類型參數(shù)
引用類型的變量不直接包含其數(shù)據(jù);它包含的是對其數(shù)據(jù)的引用。當(dāng)通過值傳遞引用類型的參數(shù)時(shí),有可能更改引用所指向的數(shù)據(jù),如某類成員的值。但是無法更改引用本身的值;也就是說,不能使用相同的引用為新類分配內(nèi)存并使之在塊外保持。若要這樣做,請使用 ref(或 out)關(guān)鍵字傳遞參數(shù)。為了簡單起見,以下示例使用 ref。
示例 4:通過值傳遞引用類型
下面的示例演示通過值向 change 方法傳遞引用類型的參數(shù) myarray。由于該參數(shù)是對 myarray 的引用,所以有可能更改數(shù)組元素的值。但是,試圖將參數(shù)重新分配到不同的內(nèi)存位置時(shí),該操作僅在方法內(nèi)有效,并不影響原始變量 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代碼討論
在上個(gè)示例中,數(shù)組 myarray 為引用類型,在未使用 ref 參數(shù)的情況下傳遞給方法。在此情況下,將向方法傳遞指向 myarray 的引用的一個(gè)副本。輸出顯示方法有可能更改數(shù)組元素的內(nèi)容(從 1 改為 888)。但是,在 change 方法內(nèi)使用 new 運(yùn)算符分配新的內(nèi)存部分,將使變量 arr 引用新的數(shù)組。因此,這之后的任何更改都不會(huì)影響原始數(shù)組 myarray(它是在 main 內(nèi)創(chuàng)建的)。實(shí)際上,本示例中創(chuàng)建了兩個(gè)數(shù)組,一個(gè)在 main 內(nèi),一個(gè)在 change 方法內(nèi)。
示例 5:通過引用傳遞引用類型
本示例除在方法頭和調(diào)用中使用 ref 關(guān)鍵字以外,其余與“示例 4”相同。方法內(nèi)發(fā)生的任何更改都會(huì)影響調(diào)用程序中的原始變量。
// 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代碼討論
方法內(nèi)發(fā)生的所有更改都影響 main 中的原始數(shù)組。實(shí)際上,使用 new 運(yùn)算符對原始數(shù)組進(jìn)行了重新分配。因此,調(diào)用 change 方法后,對 myarray 的任何引用都將指向 change 方法中創(chuàng)建的五個(gè)元素的數(shù)組。
示例 6:交換兩個(gè)字符串
交換字符串是通過引用傳遞引用類型參數(shù)的很好的示例。本示例中,str1 和 str2 兩個(gè)字符串在 main 中初始化,并作為由 ref 關(guān)鍵字修飾的參數(shù)傳遞給 swapstrings 方法。這兩個(gè)字符串在該方法內(nèi)以及 main 內(nèi)均進(jìn)行交換。
// 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代碼討論
本示例中,需要通過引用傳遞參數(shù)以影響調(diào)用程序中的變量。如果同時(shí)從方法頭和方法調(diào)用中移除 ref 關(guān)鍵字,則調(diào)用程序中不會(huì)發(fā)生任何更改。
新聞熱點(diǎn)
疑難解答
圖片精選