ref、out 修饰符

ref参数和out参数类似,除了:

1、ref参数要求在传入函数之前赋值,而out参数不用

2、out参数必须在函数结束之前被赋值,而ref参数不用

ref传递参数 若int x;则报错

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             int x=0;
 6             Foo(ref x);
 7             Console.WriteLine(x);//1
 8             Console.ReadKey();
 9         }
10         static void Foo(ref int y)
11         {
12             y = 1;
13         }
14     }

out传递参数 若去掉y=1;则报错

 1     class Program
 2     {
 3         static void Foo(out int y)
 4         {
 5             y = 1;
 6         }
 7         static void Main(string[] args)
 8         {
 9             int x;
10             Foo(out x);
11             Console.WriteLine(x);//1
12             Console.ReadKey();
13         }
14     }
原文地址:https://www.cnblogs.com/KSalomo/p/6545372.html