C#:ref和out的聯絡及區別。

來源:互聯網
上載者:User
之前學習C#時候就遇到了這個問題,不過當時沒有深究。昨晚想到這個問題時候自己嘗試敲了敲代碼,結果從啟動並執行結果來看,越看越亂。在查看了一些資料的基礎上,自己總結了一下。可能會有點亂,但是自己總結出來的東西。 一:ref 關鍵字使參數按引用傳遞。其效果是,當控制權傳遞迴調用方法時,在方法中對參數所做的任何更改都將反映在該變數中。若要使用 ref 參數,則方法定義和調用方法都必須顯式使用 ref 關鍵字。也即是說,在方法中對參數的設定和改變將會直接影響函數調用之處(代碼①及②)。無論是函數的定義還是調用時均不可忽略關鍵字ref.可以對比代碼:代碼①:

 1 class Program 2     { 3         static void Main(string[] args) 4         { 5             Program pg = new Program(); 6             int x = 10; 7             int y = 20; 8             pg.GetValue(ref x, ref  y); 9             Console.WriteLine("x={0},y={1}", x, y);10 11             Console.ReadLine();12           13         }14 15         public void GetValue(ref int x, ref int y)16         {17             x = 521;18             y = 520;19         }20     }

 

運行結果為

代碼②:

 1 class Program 2     { 3         static void Main(string[] args) 4         { 5             Program pg = new Program(); 6             int x = 10; 7             int y = 20; 8             pg.GetValue(ref x, ref  y); 9             Console.WriteLine("x={0},y={1}", x, y);10 11             Console.ReadLine();12           13         }14 15         public void GetValue(ref int x, ref int y)16         {17             x = 1000;18             y = 1;19         }20     }

 

由代碼① 和②的運行結果可以看出,在方法中對參數所做的任何更改都將反映在該變數中,而在main函數中對參數的賦值卻沒有起到作用,這是不是說明不需要進行初始化呢?來看第二點

二:ref定義的參數在使用前必須初始化,無論在函數定義時候參數有沒有賦予初值。這條正好區分out指定的參數可以不在調用函數的時候進行初始化。來看代碼③ 以及其運行結果:
 1  class Program 2     { 3         static void Main(string[] args) 4         { 5             Program pg = new Program(); 6             int x; 7             int y;        //此處x,y沒有進行初始化,則編譯不通過。 8             pg.GetValue(ref x, ref  y); 9             Console.WriteLine("x={0},y={1}", x, y);10 11             Console.ReadLine();12           13         }14 15         public void GetValue(ref int x, ref int y)16         {17             x = 1000;18             y = 1;19         }    20     }

 

出現的錯誤為:使用了未賦值的局部變數“x”,“y”。故可以說明ref指定的參數無論在函數定義的時候有沒有賦予初值,在使用的時候必須初始化。

三 :對out來說,第一條同樣適用。將代碼①以及②中的ref全部修改成out,則可與使用ref得到同樣的結果。 四:out指定的參數必須在函數定義的時候就賦初值。否則則出現錯誤。對比ref指定的參數則可以不在函數內部進行賦初值,在函數調用時候再賦初值也可以。代碼④:

 1 class Program 2     { 3         static void Main(string[] args) 4         { 5             Program pg = new Program(); 6             int x=10; 7             int y=233; 8             pg.Swap(out x, out y); 9             Console.WriteLine("x={0},y={1}", x, y);10 11             Console.ReadLine();12           13         }14 15         public void Swap(out int a,out  int b)16         {17             18             int temp = a;   //a,b在函數內部沒有賦初值,則出現錯誤。19             a = b;20             b = temp;21         }     22     23     }

 

出現錯誤:使用了未賦值的out參數“a”,"b"則在swap函數定義時候給a,b賦上初值則運行正確。 總結以上四條得到ref和out使用時的區別是:①:ref指定的參數在函數調用時候必須初始化,不可為空的引用。而out指定的參數在函數調用時候可以不初始化;②:out指定的參數在進入函數時會清空自己,必須在函數內部賦初值。而ref指定的參數不需要。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.