天天看點

ref 與out

//1. ref/out是用來修飾方法的參數的.

//2. 調用的時候 給ref/out指派 不能指派1個常量 隻能給變量.

變量前面也要加1個ref/out

//3. 在給ref/out參數指派的時候,指派的是變量的位址.

//4.

ref在方法中可以對其值不修改.

//5. ref 在傳遞之前必須指派.

static void testref(ref int

num)

{

num = num + 1;

}

//1. out必須在方法結束之前為其指派 / 在方法中如果要使用out 必須先為他指派.

//2.

out在調用之前 可以不指派 因為在方法中一定會為其指派(根據第1條)

//3. out側重于輸出.

static void

testout(out int i)

i = 12;

//寫1個方法 将1個字元串轉換int類型的. 如果轉換成功 就傳回true 并輸出轉換成功的值 如果轉換失敗 傳回false

static bool myintparse(string str, out int num)

try

num = int.parse(str);

return true;

catch

num = 0;

return false;