日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

浅谈ref与out区别

發(fā)布時間:2024/6/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅谈ref与out区别 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

今天又一次碰到了ref與out區(qū)別的問題,當(dāng)初總認為自己學(xué)的還不錯,但每次遇到后都要糾結(jié)一番,這次再次學(xué)習(xí)和鞏固一下。

MSDN中的定義:
ref 關(guān)鍵字使參數(shù)按引用傳遞。其效果是,當(dāng)控制權(quán)傳遞回調(diào)用方法時,在方法中對參數(shù)所做的任何更改都將反映在該變量中。若要使用 ref 參數(shù),則方法定義和調(diào)用方法都必須顯式使用 ref 關(guān)鍵字。
?out 關(guān)鍵字會導(dǎo)致參數(shù)通過引用來傳遞。這與 ref 關(guān)鍵字類似,不同之處在于 ref 要求變量必須在傳遞之前進行初始化。若要使用 out 參數(shù),方法定義和調(diào)用方法都必須顯式使用 out 關(guān)鍵字。

首先,我們來看一個簡單的例子:

??????? static void TestRefAndOut()
??????? {
??????????? string s1 = "Good Luck!";
??????????? TestRef(ref s1);
??????????? Console.WriteLine(s1);//output: Hello World!
??????? }
??????? static void TestRef(ref string str)
??????? {
??????????? str = "Hello World!";
??????? }
在TestRefAndOut()中將字符串s1以ref關(guān)鍵字的方式傳到方法TestRef(ref string str)中,在這個方法中,我們改變了s1的引用變量str的值,最后,回到TestRefAndOut()方法后輸出s1的值,發(fā)現(xiàn)其值已被改變。

將上例中的ref換成out,代碼如下:

??????? static void TestRefAndOut()
??????? {
??????????? string s1 = "Good Luck!";
??????????? //TestRef(ref s1);
??????????? TestOut(out s1);
??????????? Console.WriteLine(s1);//output: Hello World!
??????? }

??????? static void TestOut(out string str)
??????? {
??????????? str = "Hello World!";
??????? }
同樣,在將ref換成out后,會發(fā)現(xiàn)最后的輸出仍然是相同的,那這兩個關(guān)鍵字的區(qū)別是什么呢?

進一步測試:

ref:

??????? static void TestRefAndOut()
??????? {
??????????? string s1 = "Good Luck!";
??????????? TestRef(ref s1);
??????? }

??????? static void TestRef(ref string str)
??????? {
??????????? Console.WriteLine(str);//output: Good Lick!???????????
??????? }
?out

??????? static void TestRefAndOut()
??????? {
??????????? string s1 = "Good Luck!";
??????????? TestOut(out s1);
??????? }

??????? static void TestOut(out string str)
??????? {
??????????? Console.WriteLine(str);//compile does not pass
??????? }
ref的那段代碼順利編譯,輸出"Good Luck!",而out那段代碼卻無法通過編譯,提示“Use of unassigned out parameter 'str' ”,即使用了未分配地址的out參數(shù)str。怎么回事呢?

原來out參數(shù)在進入方法的時候,C#會自動清空它的一切引用和指向,所以在上面的out例子中,必需先要為str參數(shù)賦值。如以下程序。

??????? static void TestRefAndOut()
??????? {
??????????? string s1 = "Good Luck!";
??????????? TestOut(out s1);
??????? }
??????? static void TestOut(out string str)
??????? {
??????????? str = "Hello World!";
??????????? Console.WriteLine(str);//output: Hello World!
??????? }
Ok,得到第一個區(qū)別: out 參數(shù)在進入方法(函數(shù))時后清空自己,使自己變成一個干凈的參數(shù),也因為這個原因必須在方法返回之前或再使用out參數(shù)前為 out 參數(shù)賦值(只有地址沒有值的參數(shù)是不能被.net接受的);而ref參數(shù)是不需要在被調(diào)用方法使用前先賦值的,甚至也可以被調(diào)用方法中不改變ref參數(shù)的值,這都不會引起編譯錯誤。

在繼續(xù)看一段代碼:

ref:

??????? static void TestRefAndOut()
??????? {
??????????? string s1;
??????????? TestRef(ref s1);
??????????? Console.WriteLine(s1);//compile does not pass!
??????? }
??????? static void TestRef(ref string str)
??????? {
??????????? str = Hello World!";
??????? }???
out:
??????? static void TestRefAndOut()
??????? {
??????????? string s1;
??????????? TestOut(out s1);
??????????? Console.WriteLine(s1);//output: Hello World!
??????? }

??????? static void TestOut(out string str)
??????? {
??????????? str = "Hello World!";
??????? }??
這回發(fā)現(xiàn),ref這段代碼無法編譯了,s1是一個空引用,所以無法使用。而out參數(shù)則因為上述的那個原因,它不在乎s1是不是空引用,因為就算s1不是空引用,它也會把s1變成空引用的。Ok,第二個區(qū)別:ref參數(shù)在使用前必需初始化,而out不需要。嗯,由上邊兩個區(qū)別可以引申一下,out參數(shù)只進不出,ref參數(shù)有進有出。在用法上概括一下就是:out適合用在需要retrun多個返回值的地方,而ref則用在需要被調(diào)用的方法修改調(diào)用者的引用的時候。

總結(jié)一下:

首先:兩者都是按地址傳遞的,使用后都將改變原來的數(shù)值。很多人在論壇上解釋說out是按數(shù)值傳遞,是錯誤的。簡單的測試后可以知道out使用也能改變數(shù)值的,所以肯定是按照地址傳遞的。
其次:rel可以把參數(shù)的數(shù)值傳遞進函數(shù),但是out是要把參數(shù)清空,就是說你無法把一個數(shù)值從out傳遞進去的,out進去后,參數(shù)的數(shù)值為空,所以你必須初始化一次。這個就是兩個的區(qū)別,或者說就像有的網(wǎng)友說的,rel是有進有出,out是只出不進。

所以也就不難得出以下這道題的答案了:

class TestApp

??? {

??????? static void outTest(out int x, ref int y)

??????? {

??????????? x = 1;

??????????? y = 2;

??????? }

??????? static void refTest(ref int x, out int y)

??????? {

??????????? x = 1;

??????????? y = x;

??????? }

??????? public static void Main()

??????? {

??????????? int a = 1;

??????????? int b = 3;

??????????? outTest(out a, ref b);

??????????? outTest(out a, ref b);

??????????? refTest(ref a, out b);

??????????? Console.WriteLine("a={0};b={1}", a, b);

??????????? Console.ReadKey();

??????? }

??? }

?

轉(zhuǎn)載于:https://www.cnblogs.com/skyer-2013/archive/2013/02/18/2916231.html

總結(jié)

以上是生活随笔為你收集整理的浅谈ref与out区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。