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;
}
}

原文地址:https://www.cnblogs.com/zhang123/p/3705604.html