输出参数out和引用参数ref区别2011-06-26 博客园 贤使用ref的一段代码using System; class M { public void F(ref int i) { i=3; } } class Test { int i=0; //要作为引用参数传递的变量必须明确赋值 static void Main() { //不能把int i=0;放在这里赋值,会报错说Test不包含i定义。 Test t=new Test(); Console.WriteLine("the value of i is:{0}",t.i); M mm=new M(); mm.F(ref t.i); //作为引用参数传递 Console.WriteLine("now the value of i is :{0}",t.i); //i的值改变 } }使用out的一段类似代码class M { public void F(out int i) //这个方法和ref的方法都是一样,没什么不同 { i = 8; //返回前必须明确赋值 } } class Test { int i; //不用赋初值,这就是out和ref的区别,但声明还是要的 public static void Main() { Test t1 = new Test(); Console.WriteLine("the value of i is :{0}", t1.i); //输出是0; M m1 = new M(); m1.F(out t1.i); //i作为输出参数传递 ,输出是8 Console.WriteLine("now value of i is :{0}", t1.i); } }