ref
??? 近段時間,有幾個剛剛開始學習C#語言的愛好者問我:C#中的函數,其參數的傳遞,按值傳遞和按引用傳遞有什么區別。針對這一問題,我簡單寫了個示例程序,用以講解,希望我沒有把他們繞暈。因為,常聽別人說起:“你不說我還明白,你一說,我就糊涂了”。
??? 好,現在開始吧。
??? 我們知道,在C#中,類型有值類型(例如int)和引用類型(例如string)之分,傳遞參數有按值傳遞和按引用傳遞之分。這樣,簡單的組合一下,我們可以得到以下幾種傳遞方式:(1)按值傳遞值類型。(2)按值傳遞引用類型。(3)按引用傳遞值類型。(4)按引用傳遞引用類型。一般來說,除非使用特定的關鍵字(ref和out)否則參數是按值傳遞的。也就是說,會傳遞一個副本。傳遞副本的一個好處是,可以避免誤操作而影響了原始值。原因是在被調用的函數體內,操作的是副本的值,而不是原始值。當然,傳遞副本也是有副作用的,最為突出的應該是由于復制而產生的性能損耗,這點在大型的值類型身上尤為突出。那么C#的編譯器的默認行為為什么不是使用按引用傳遞參數呢?呵呵,其實我沒仔細深入思考過這個問題。我猜測,是因為安全因素吧,就是怕函數誤操作了原始值。這點應該和C#的編譯器要求顯示使用關鍵字(ref和out)差不多,都是為了清楚地表達使用的意圖,以避免誤操作。使用ref等關鍵字,暗示函數調用者知道,在函數體內,也許存在修改原始值的語句,會改變參數的值(或者叫狀態)。
??? 用個簡單的示例演示一下。
??? 示例代碼如下所示:
???
??1using?System;
??2
??3namespace?DonLiang
??4{
??5????class?Sample
??6????{
??7????????值類型測試函數#region?值類型測試函數
??8????????public?static?void?foo(int?x)
??9????????{
?10????????????x?=?10;
?11????????}
?12
?13????????//*
?14????????public?static?void?foo(ref?int?x)
?15????????{
?16????????????x?=?10;
?17????????}
?18????????//*/
?19
?20????????/**//*
?21????????public?static?void?foo(out?int?x)
?22????????{
?23????????????x?=?10;
?24????????}
?25????????//*/
?26????????#endregion
?27
?28????????輔助引用類型#region?輔助引用類型
?29????????public?class?Point
?30????????{
?31????????????private?int?m_x;
?32????????????private?int?m_y;
?33
?34????????????public?Point()
?35????????????{
?36????????????????m_x?=?0;
?37????????????????m_y?=?0;
?38????????????}
?39
?40????????????public?Point(int?x,?int?y)
?41????????????{
?42????????????????m_x?=?x;
?43????????????????m_y?=?y;
?44????????????}
?45
?46????????????public?void?Change(int?x,?int?y)
?47????????????{
?48????????????????m_x?=?x;
?49????????????????m_y?=?y;
?50????????????}
?51
?52????????????public?override?string?ToString()
?53????????????{
?54????????????????return?string.Format("The?Point?is?({0},{1})",?m_x.ToString(),?m_y.ToString());
?55????????????}
?56????????}
?57????????#endregion
?58
?59????????引用類型測試函數#region?引用類型測試函數
?60????????public?static?void?foo(Point?p)
?61????????{
?62????????????p.Change(10,?10);
?63????????}
?64
?65????????public?static?void?foo(ref?Point?p)
?66????????{
?67????????????p.Change(100,?100);
?68????????}
?69
?70????????public?static?void?other(Point?p)
?71????????{
?72????????????Point?tmp?=?new?Point(13,?16);
?73????????????p?=?tmp;
?74????????}
?75
?76????????public?static?void?other(ref?Point?p)
?77????????{
?78????????????Point?tmp?=?new?Point(138,?168);
?79????????????p?=?tmp;
?80????????}
?81????????#endregion
?82
?83????????Main#region?Main
?84????????static?void?Main(string[]?args)
?85????????{
?86????????????int?n?=?5;
?87
?88????????????//call?the?foo(int?x)?method?and?check?what?happened.
?89????????????Console.WriteLine("before?call?foo(int?x)?the?n?=?"?+?n.ToString());
?90????????????foo(n);
?91????????????Console.WriteLine("after?call?foo(int?x)?the?n?=?"?+?n.ToString());
?92
?93????????????Console.WriteLine("--------------------------------------------------------------");
?94
?95????????????//call?the?foo(ref?int?x)?method?and?check?what?happened.
?96????????????Console.WriteLine("before?call?foo(ref?int?x)?the?n?=?"?+?n.ToString());
?97????????????foo(ref?n);
?98????????????//foo(out?n);
?99????????????Console.WriteLine("after?call?foo(ref?int?x)?the?n?=?"?+?n.ToString());
100
101????????????Console.WriteLine("--------------------------------------------------------------");
102
103????????????Point?p?=?new?Point(5,?5);
104????????????Point?q?=?p;
105
106????????????//call?the?foo(Point?p)?method?and?check?what?happened.
107????????????Console.WriteLine("before?call?foo(Point?p)?the?p?=?"?+?p.ToString());
108????????????foo(p);
109????????????Console.WriteLine("after?call?foo(Point?p)?the?p?=?"?+?p.ToString());
110????????????Console.WriteLine("q?=?"?+?q.ToString());
111
112????????????Console.WriteLine("--------------------------------------------------------------");
113
114????????????//call?the?foo(ref?Point?p)?method?and?check?what?happened.
115????????????Console.WriteLine("before?call?foo(ref?Point?p)?the?n?=?"?+?p.ToString());
116????????????foo(ref?p);
117????????????Console.WriteLine("after?call?foo(ref?Point?p)?the?n?=?"?+?p.ToString());
118????????????Console.WriteLine("q?=?"?+?q.ToString());
119
120????????????Console.WriteLine("--------------------------------------------------------------");
121
122????????????//call?the?other(Point?p)?method?and?check?what?happened.
123????????????Console.WriteLine("before?call?other(Point?p)?the?n?=?"?+?p.ToString());
124????????????other(p);
125????????????Console.WriteLine("after?call?other(Point?p)?the?n?=?"?+?p.ToString());
126????????????Console.WriteLine("q?=?"?+?q.ToString());
127
128????????????Console.WriteLine("--------------------------------------------------------------");
129
130????????????//call?the?other(ref?Point?p)?method?and?check?what?happened.
131????????????Console.WriteLine("before?call?other(ref?Point?p)?the?n?=?"?+?p.ToString());
132????????????other(ref?p);
133????????????Console.WriteLine("after?call?other(ref?Point?p)?the?n?=?"?+?p.ToString());
134????????????Console.WriteLine("q?=?"?+?q.ToString());
135
136????????????Console.ReadLine();
137????????}
138????????#endregion
139????}
140}
??? 程序的運行結果也是顯而易見的,如下圖所示:
???
??? 接下來,簡單分析一下這個結果:
??? (1)按值傳遞值類型
??? 初始值為5,調用函數的時候,弄了個副本給函數折騰,于是,從函數返回后,值還是5。嗯,本來應該弄個堆棧圖出來的,可是,圖是在太難畫,因此,我偷懶,用VS2008的調試監視器看看:
???
??? (2)按引用傳遞值類型
??? 初始值還是5,這次換了個傳遞參數的方式——按引用傳遞,這次可不是副本了,而是原始值(的地址),這就類似大牌的武打演員總不能老是使用替身一樣,偶爾還是要親自上陣的。既然是親自上陣,那么,值被修改為10就不足為奇了。正如結果圖所示,n=10了。
???
??? (3)按值傳遞引用類型 和 按引用傳遞引用類型
??? 之所以把這兩個放在一起講,是因為,如結果圖所示,兩種傳遞方式,都成功修改了值——這兩個函數都分別調用了一個輔助修改的函數Change,去修改內部狀態,即m_x,m_y的值,從5到10。呃,竟然都可以成功修改原始值,那么,為什么會存在兩種方式呢?它們有什么區別嗎?分別用在什么地方?為了說明他們的區別,我特意寫了兩個名為other的函數,在函數內new一個Point對象,并使從參數傳遞過來的引用這個新生成的Point對象。值得提醒的是,這個引用其定義在函數體外。其運行如上圖我用方框框起來那個。
??? 可以很清楚地看到,通過值傳遞方式,可以改變其值,卻不能改變其本身所引用的對象;而按引用傳遞方式可以。
??? 順便提一下,代碼中,有一段注釋掉的代碼,使用out關鍵字的。當你嘗試將其兩者一起寫著,然后,編譯,C#編譯器是會提示錯誤的(error CS0663: 'foo' cannot define overloaded methods that differ only on ref and out)。其原因是,C#編譯器,對ref和out生成的IL代碼,是相同的;而在CLR層面,是沒有ref和out的區別的。C#中,ref和out的區別,主要是,誰負責初始化這個參數使之能用——ref形式是函數外初始化,而out是函數內初始化。
??? 希望本文對你有所幫助。
轉載于:https://www.cnblogs.com/xbs729/archive/2010/05/06/1729130.html
總結
- 上一篇: C语言实现tolower
- 下一篇: 掌握 Ajax,第 8 部分: 在请求和