c#中通過值和引用傳遞參數

http://www.webasp.net/article/28/27438_print.htm 

在 C# 中,既可以通過值也可以通過引用傳遞參數。通過引用傳遞參數允許函數成員(方法、屬性、索引器、運算符和構造函數)更改參數的值,並保持該更改。若要通過引用傳遞參數,請使用 ref 或 out 關鍵字。爲簡單起見,本主題的示例中只使用了 ref 關鍵字。有關 ref 和 out 之間的差異的信息,請參見、使用 ref 和 out 傳遞數組。

本主題包括下列章節:

  • 傳遞值類型參數
  • 傳遞引用類型參數

它還包括以下示例:

示例 演示 是否使用 ref 或 out
1 通過值傳遞值類型
2 通過引用傳遞值類型
3 交換值類型(兩個整數)
4 通過值傳遞引用類型
5 通過引用傳遞引用類型
6 交換引用類型(兩個字符串)

摘自:c#中通過值和引用傳遞參數(downmoon)


 

 
 

傳遞值類型參數

值類型變量直接包含其數據,這與引用類型變量不同,後者包含對其數據的引用。因此,向方法傳遞值類型變量意味着向方法傳遞變量的一個副本。方法內發生的對參數的更改對該變量中存儲的原始數據無任何影響。如果希望所調用的方法更改參數值,必須使用 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: 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: {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: 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: {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: 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: {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: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3

 

 

示例 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: {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 Smith
Inside the method: Smith, John
Inside Main, after swapping: Smith, John

代碼討論


本示例中,需要通過引用傳遞參數以影響調用程序中的變量。如果同時從方法頭和方法調用中移除 ref 關鍵字,則調用程序中不會發生任何更改。


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章