ref out param 区别
如果在方法聲明參數中未使用ref或out關鍵字,在方法中更改參數的值,當控制傳遞回調用過程時,不會保留更改的值;我們可以通過方法參數關鍵字,更改這種行為。
1.ref
2.out
3.params
ref和out關鍵字都可以使參數按引用傳遞,當控制權傳遞回調用方時,在被調方法中對參數所做的任何修改都將反映在該變量中,在使用時,都要求方法定義和調用方法顯式使用ref或out關鍵字,但是它們有如下的幾個區別:
a.當使用ref關鍵字時,傳遞到ref參數的變量必須事先初始化,而與 out不同,out 的參數在傳遞前不需要初始化,請看示例:?static void Main(string[] args)
??????? {
??????????? string aa = "0";? //傳遞給ref參數前,必須先初始化
??????????? TestRef(ref aa);
??????????? Console.WriteLine(aa);
???????? }??public static void TestRef(ref string refTest)
??????? {
??????????? refTest = "1";
???????? }
結果輸出 "1"?static void Main(string[] args)
??????? {
????????????string aa;? //傳遞給out參數前,不必先初始化
??????????? TestRef(out aa);
??????????? Console.WriteLine(aa);
???????? }?public static void TestRef(out string refTest)
??????? {
??????????? refTest = "1";
???????? }
結果輸出"1"
ref和out在運行時的處理方式不同,但是在編譯的時候的處理方式確實相同的,所以下面的兩個函數是相同的
public void SampleMethod(ref int i) {? }
public void SampleMethod(out int i) {? }
在使用ref或out傳遞數組參數時,我們也要注意:
使用out傳遞時,在被調用方法中需要對數組進行賦值,這個是需要注意的地方;使用ref時,和上述的要求一樣,需要先進行初始化,即由調用方明確賦值,所以不需要由被調用方明確賦值,請看代碼:? static void Main(string[] args)
??????? {
??????????? string[] aa =null;//明確賦值
??????????? TestRef(ref aa);
??????????? Console.WriteLine((aa!=null&&aa.Length>0)?aa[0]:"null");
??????? }?public static void TestRef(ref string[] refTest)
??????? {
?????????? //這里不要明確賦值
??????????? if (refTest != null)
??????????? {
??????????????? if (refTest.Length > 0)
??????????????? {
??????????????????? refTest[0] = "A";
??????????????? }
??????????? }
??????? }?static void Main(string[] args)
??????? {
??????????? string[] aa ;//
??????????? TestRef(out aa);
??????????? Console.WriteLine((aa!=null&&aa.Length>0)?aa[0]:"null");
??????? }
??????? public static void TestRef(out string[] refTest)
??????? {
??????????? refTest = new string[] { "a", "b", "c", "d" };?//由于out參數不需要在傳遞前進行初始化,這里需要對對參數進行賦值
??????????? if (refTest != null)
??????????? {
??????????????? if (refTest.Length > 0)
??????????????? {
??????????????????? refTest[0] = "A";
??????????????? }
??????????? }
???????? }
使用out參數,我們可以讓方法有多個返回值,如:
?static void Method(out int i, out string s1, out string s2)
??? {
??????? i = 44;
??????? s1 = "I've been returned";
??????? s2 = null;
??? }
??? static void Main()
??? {
??????? int value;
??????? string str1, str2;
??????? Method(out value, out str1, out str2);
??????? // value is now 44
??????? // str1 is now "I've been returned"
??????? // str2 is (still) null;
??? }
params關鍵字:params 關鍵字可以指定在參數數目可變處采用參數的方法參數,在使用時要注意幾點:
1.在方法聲明中的params關鍵字后,不允許再出現其他參數
2.在方法聲明中只允許使用一個params關鍵字
?static void Main(string[] args)
??????? { TestParams(1, "a", "cc");//方式1
??????????? object[] obj = new object[] {1,"a","c" };//數組也可以傳遞過去,只要類型匹配
??????????? TestParams(obj);
???????? }
?public static void TestParams(params object[] para)
??????? {
??????????? for (int i = 0; i < para.Length; i++)
??????????? {
??????????????? Console.WriteLine(para[i]);
??????????? }
??????? }
總結
以上是生活随笔為你收集整理的ref out param 区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL语句执行顺序以及oracle基本查
- 下一篇: Ajax中文乱码问题解决方法(服务器端用