C#中的值傳遞和引用傳遞詳細(xì)解析
一、傳遞參數(shù)
既可以通過(guò)值也可以通過(guò)引用傳遞參數(shù)。通過(guò)引用傳遞參數(shù)允許函數(shù)成員(方法、屬性、索引器、運(yùn)算符和構(gòu)造函數(shù))更改參數(shù)的值,并保持該更改。
二、傳遞值類(lèi)型參數(shù)
值類(lèi)型變量直接包含其數(shù)據(jù),這與引用類(lèi)型變量不同,后者包含對(duì)其數(shù)據(jù)的引用。因此,向方法傳遞值類(lèi)型變量意味著向方法傳遞變量的一個(gè)副本。方法內(nèi)發(fā)生的對(duì)參數(shù)的更改對(duì)該變量中存儲(chǔ)的原始數(shù)據(jù)無(wú)任何影響。如果希望所調(diào)用的方法更改參數(shù)的值,必須使用 ref 或 out 關(guān)鍵字通過(guò)引用傳遞該參數(shù)。為了簡(jiǎn)單起見(jiàn),下面的示例使用 ref。
1. 通過(guò)值傳遞值類(lèi)型:
class PassingValByVal
{
static void SquareIt(int x)
// The parameter x is passed by value.
// Changes to x will not affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The value before calling the method: {0}", n);
SquareIt(n); // Passing the variable by value.
System.Console.WriteLine("The value after calling the method: {0}", n);
}
}
變量 n 為值類(lèi)型,包含其數(shù)據(jù)(值為 5)。當(dāng)調(diào)用 SquareIt 時(shí),n 的內(nèi)容被復(fù)制到參數(shù) x 中,在方法內(nèi)將該參數(shù)求平方。但在 Main 中,n 的值在調(diào)用 SquareIt 方法前后是相同的。實(shí)際上,方法內(nèi)發(fā)生的更改只影響局部變量 x。
2.通過(guò)引用傳遞值類(lèi)型
下面的示例除使用 ref 關(guān)鍵字傳遞參數(shù)以外,其余與上一示例相同。參數(shù)的值在調(diào)用方法后發(fā)生更改
class PassingValByRef
{
static void SquareIt(ref int x)
// The parameter x is passed by reference.
// Changes to x will affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The value before calling the method: {0}", n);
SquareIt(ref n); // Passing the variable by reference.
System.Console.WriteLine("The value after calling the method: {0}", n);
}
}
本示例中,傳遞的不是 n 的值,而是對(duì) n 的引用。參數(shù) x 不是 int 類(lèi)型,它是對(duì) int 的引用(本例中為對(duì) n 的引用)。因此,當(dāng)在方法內(nèi)對(duì) x 求平方時(shí),實(shí)際被求平方的是 x 所引用的項(xiàng):n。
3. 交換值類(lèi)型
更改所傳遞參數(shù)的值的常見(jiàn)示例是 Swap 方法,在該方法中傳遞 x 和 y 兩個(gè)變量,然后使方法交換它們的內(nèi)容。必須通過(guò)引用向 Swap 方法傳遞參數(shù);否則,方法內(nèi)所處理的將是參數(shù)的本地副本。以下是使用引用參數(shù)的 Swap 方法的示例:
static void SwapByRef(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
三、傳遞引用類(lèi)型參數(shù)
引用類(lèi)型的變量不直接包含其數(shù)據(jù);它包含的是對(duì)其數(shù)據(jù)的引用。當(dāng)通過(guò)值傳遞引用類(lèi)型的參數(shù)時(shí),有可能更改引用所指向的數(shù)據(jù),如某類(lèi)成員的值。但是無(wú)法更改引用本身的值;也就是說(shuō),不能使用相同的引用為新類(lèi)分配內(nèi)存并使之在塊外保持。若要這樣做,應(yīng)使用 ref 或 out 關(guān)鍵字傳遞參數(shù)。為了簡(jiǎn)單起見(jiàn),下面的示例使用 ref。
1. 通過(guò)值傳遞引用類(lèi)型
下面的示例演示通過(guò)值向 Change 方法傳遞引用類(lèi)型的參數(shù) arr。由于該參數(shù)是對(duì) arr 的引用,所以有可能更改數(shù)組元素的值。但是,試圖將參數(shù)重新分配到不同的內(nèi)存位置時(shí),該操作僅在方法內(nèi)有效,并不影響原始變量 arr。
class PassingRefByVal
{
static void Change(int[] pArray)
{
pArray[0] = 888; // This change affects the original element.
pArray = new int[5] {-3, -1, -2, -3, -4}; // This change is local.
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
}
static void Main()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);
Change(arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);
}
}
在上個(gè)示例中,數(shù)組 arr 為引用類(lèi)型,在未使用 ref 參數(shù)的情況下傳遞給方法。在此情況下,將向方法傳遞指向 arr 的引用的一個(gè)副本。輸出顯示方法有可能更改數(shù)組元素的內(nèi)容,在這種情況下,從 1改為 888。但是,在 Change 方法內(nèi)使用 new 運(yùn)算符來(lái)分配新的內(nèi)存部分,將使變量 pArray 引用新的數(shù)組。因此,這之后的任何更改都不會(huì)影響原始數(shù)組 arr(它是在 Main 內(nèi)創(chuàng)建的)。實(shí)際上,本示例中創(chuàng)建了兩個(gè)數(shù)組,一個(gè)在 Main 內(nèi),一個(gè)在 Change 方法內(nèi)。
2. 通過(guò)引用傳遞引用類(lèi)型
本示例除在方法頭和調(diào)用中使用 ref 關(guān)鍵字以外,其余與上個(gè)示例相同。方法內(nèi)發(fā)生的任何更改都會(huì)影響調(diào)用程序中的原始變量
class PassingRefByRef
{
static void Change(ref int[] pArray)
{
// Both of the following changes will affect the original variables:
pArray[0] = 888;
pArray = new int[5] {-3, -1, -2, -3, -4};
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
}
static void Main()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);
Change(ref arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);
}
}
方法內(nèi)發(fā)生的所有更改都影響 Main 中的原始數(shù)組。實(shí)際上,使用 new 運(yùn)算符對(duì)原始數(shù)組進(jìn)行了重新分配。因此,調(diào)用 Change 方法后,對(duì) arr 的任何引用都將指向 Change 方法中創(chuàng)建的五個(gè)元素的數(shù)組。
3. 交換兩個(gè)字符串
交換字符串是通過(guò)引用傳遞引用類(lèi)型參數(shù)的很好的示例。本示例中,str1 和 str2 兩個(gè)字符串在 Main 中初始化,并作為由 ref 關(guān)鍵字修改的參數(shù)傳遞給 SwapStrings 方法。這兩個(gè)字符串在該方法內(nèi)以及Main 內(nèi)均進(jìn)行交換。
class SwappingStrings
{
static void SwapStrings(ref string s1, ref string s2)
// The string parameter is passed by reference.
// Any changes on parameters will affect the original variables.
{
string temp = s1;
s1 = s2;
s2 = temp;
System.Console.WriteLine("Inside the method: {0} {1}", s1, s2);
}
static void Main()
{
string str1 = "John";
string str2 = "Smith";
System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);
SwapStrings(ref str1, ref str2); // Passing strings by reference
System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
}
}
本示例中,需要通過(guò)引用傳遞參數(shù)以影響調(diào)用程序中的變量。如果同時(shí)從方法頭和方法調(diào)用中移除 ref 關(guān)鍵字,則調(diào)用程序中不會(huì)發(fā)生任何更改。
四、引用類(lèi)型的數(shù)據(jù)值傳遞(復(fù)本傳遞)
類(lèi)的默認(rèn)用MemberwiseClone 方法創(chuàng)建一個(gè)淺表副本,方法是創(chuàng)建一個(gè)新對(duì)象,然后將當(dāng)前對(duì)象的非靜態(tài)字段復(fù)制到該新對(duì)象。如果字段是值類(lèi)型的,則對(duì)該字段執(zhí)行逐位復(fù)制。如果字段是引用 類(lèi)型,則復(fù)制引用但不復(fù)制引用的對(duì)象;因此,原始對(duì)象及其復(fù)本引用同一對(duì)象。深拷貝,即實(shí)現(xiàn)ICloneable接口.ICloneable可用于深拷貝 和淺拷貝。這些都是概念,但是需要我們理解:
class ClassA : ICloneable
{
public string str;
public SubClass subclass;
public ClassA()
{
str = "classA str";
subclass = new SubClass();
}
//深復(fù)制,多層不可用MemberwiseClone()完整實(shí)現(xiàn)深復(fù)制
public object Clone()
{
// this.a = (string)this.a.Clone();
//this.b = (B)this.b.Clone();
var ne = new ClassA();
ne.str = this.str;
ne.subclass = (SubClass)this.subclass.Clone(); //this.b的話(huà)還是沒(méi)有成功
return ne;
// return this.MemberwiseClone();
}
}
class SubClass : ICloneable
{
public string str;
public SubClass()
{
this.str = "subclass str";
}
//深復(fù)制,因?yàn)橹灰粚?,所以可以用MemberwiseClone()方法
public object Clone()
{
this.str = (string)this.str.Clone();
return this.MemberwiseClone();
}
相關(guān)文章
在WPF中合并兩個(gè)ObservableCollection集合
這篇文章介紹了在WPF中合并兩個(gè)ObservableCollection集合的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06C#實(shí)現(xiàn)簡(jiǎn)單的點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單的點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C#中緩存System.Web.Caching用法總結(jié)
本文詳細(xì)講解了C#中緩存System.Web.Caching的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04c#根據(jù)文件類(lèi)型獲取相關(guān)類(lèi)型圖標(biāo)的方法代碼
c#根據(jù)文件類(lèi)型獲取相關(guān)類(lèi)型圖標(biāo)的方法代碼,需要的朋友可以參考一下2013-05-05C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實(shí)例詳解
這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了順序表的定義、原理與具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11C#從windows剪貼板獲取并顯示文本內(nèi)容的方法
這篇文章主要介紹了C#從windows剪貼板獲取并顯示文本內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04詳解C#中==、Equals、ReferenceEquals的區(qū)別
C#中Equals , == , ReferenceEquals都可以用于判斷兩個(gè)對(duì)象的個(gè)體是不是相等,本篇文章詳解C#中Equals , == , ReferenceEquals都可以用于判斷兩個(gè)對(duì)象的個(gè)體是不是相等,有興趣的可以了解一下。2016-12-12