c#为了传引用引入了ref和out,ref和out唯一区别就是,ref可以传入外部的值,out不可以,必须在函数里赋值
class Program
{
static void Main(string[] args)
{
int a = 3;
int b = 4;
fun(ref a, ref b);
fun2(out a, out b);
Console.WriteLine();
}
static void fun(ref int a, ref int b)
{
a = a + b;
b = 10;
}
static void fun2(out int a, out int b)
{
a = a + b; //报错因为out必须在函数里赋值,外部的值无法传入
}
}