本帖最后由 Cacerine 于 2014-6-27 15:50 编辑  
-     static void Main(string[] args)
 
 -         {
 
 -             //定义3个变量,并赋值
 
 -             int a = 5,b=10,c=20;
 
 -             //第一次输出
 
 -             Console.WriteLine("{0},{1},{2}", a, b, c);
 
 -             int result = fangfa(a, out b, ref c);
 
 -             //调用方法后的输出(第3次输出)
 
 -             Console.WriteLine("{0},{1},{2}",result,b,c);
 
 -             Console.ReadKey();
 
  
-         }
 
 -         static int  fangfa(int canshu1,out int canshu2, ref  int canshu3)
 
 -         {
 
 -             //这里的canshu2是无法进行输出的,提示:使用了未赋值的out 参数 canshu2
 
 -             Console.WriteLine("{0},{1},{2}", canshu1, canshu2, canshu3);
 
 -             canshu1 = canshu1 + 1;
 
 -             //注意看:canshu2在调用时已经被赋值,只是在参数前面加了out后,canshu2的值被清空了,所以在这里必须重新赋值,然后才能使用
 
 -             canshu2 = 10;
 
 -             //当屏蔽掉canshu2的重新赋值后,报错(提示:使用了未赋值的out 参数 canshu2)
 
 -             canshu2 = canshu2 + 1;
 
 -             canshu3 = canshu3 + 1;
 
 -             //第二次输出
 
 -             Console.WriteLine("{0},{1},{2}", canshu1, canshu2, canshu3);
 
 -             return canshu1;
 
 -         }
 
  复制代码 我不知道你明不明白我的意思,你把这个程序调一遍,就发现 out  和 ref  的区别在叫做fangfa的函数中,写一个输出,会发现,由out 修饰的 参数下面会有波浪线,提示,未赋值  ,也就是说  ,使用out 修饰的参数无法将调用时的参数值传递到本函数中,而  ref则不存在此问题   
结论: 
out  无法传值进函数,但是可以把值传出去给调用者 
ref   可以传值进函数 ,并且可以把值传出去给调用者 
 
 |