天天看点

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;