1.三种函数调用
ref 和 out 是引用的形式传递参数,有别于传统的值传递
private static void Main(string[] args)
{
int _mref = 1;
int _mout ;
int _m = 3;
MethodRef(ref _mref);
MethodOut(out _mout);
Method(_m);
}
public static void MethodRef(ref int a){print(a);}
public static void MethodOut(out int a){a = 2;print(a);}
public static void Method(int a = 1){}
2.差异比较
out
- 返回out修饰的变量之前,该值必须是被初始化;
- 而在调用函数之前该变量无需初始化;
- 允许返回多个值;
ref
- 调用函数前变量必须初始化;
- 可用于交换函数 swap(ref int a, ref int b)
3.相同点
- 全都是通过引用传递;
- 对于CLR,ref和out除了标识,中间代码和元数据都是相同的,这导致ref和out无法重载彼此;
- 调用函数不可以有默认值;