c#中out、ref和params的用法与区别

来源:互联网 发布:linux iic 编辑:程序博客网 时间:2024/05/18 00:36

      ref和out都对函数参数采用引用传递形式——不管是值类型参数还是引用类型参数,并且定义函数和调用函数时都必须显示生命该参数为ref/out形式。两者都可以使函数传回多个结果。

      两者区别:

      两种参数类型的设计思想不同,ref的目的在于将值类型参数当作引用型参数传递到函数,是函数的输入参数,并且在函数内部的任何改变也都将影响函数外部该参数的值;而out的目的在于获取函数的返回值,是输出参数,由函数内部计算得到的值再回传到函数外部,因此必须在函数内部对该参数赋值,这将冲掉函数外部的任何赋值,使得函数外部赋值毫无意义。

表现为:

      1、out必须在函数体内初始化,这使得在外面初始化变得没意义。也就是说,out型的参数在函数体内不能得到外面传进来的初始值。
      2、ref必须在函数体外初始化。
      3、两者在函数体内的任何修改都将影响到函数体外面。

 

例:

using System;

namespace ConsoleApplication1
{
       class C
       {
              public static void reffun(ref string str)
              {
                      str += " fun";
              }

              public static void outfun(out string str)
              {
                      str = "test"; //必须在函数体内初始, 如无此句,则下句无法执行,报错
                      str += " fun";
              }
       }

       class Class1
       {
                [STAThread]
                static void Main(string[] args)
                {
                        string test1 = "test";
                        string test2; //没有初始
                        C.reffun( ref test1 ); //正确
                        C.reffun( ref test2 ); //错误,没有赋值使用了test2
                        C.outfun( out test1 ); //正确,但值test无法传进去
                        C.outfun( out test2 ); //正确

                        Console.Read();
                 }
         }
}

 

      params 关键字可以指定在参数数目可变处采用参数的方法参数。

      在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。

      The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

      No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

 

// cs_params.cs
using System;
public class MyClass
{

        public static void UseParams(params int[] list)
        {
               for (int i = 0 ; i < list.Length; i++)
               {
                       Console.WriteLine(list[i]);
               }
               Console.WriteLine();
         }

         public static void UseParams2(params object[] list)
         {
                for (int i = 0 ; i < list.Length; i++)
                {
                        Console.WriteLine(list[i]);
                }
                Console.WriteLine();
          }

          static void Main()
          {
                 UseParams(1, 2, 3);
                 UseParams2(1, 'a', "test");

                 // An array of objects can also be passed, as long as
                 // the array type matches the method being called.
                 int[] myarray = new int[3] {10,11,12};
                 UseParams(myarray);
      }
}

 

Output

1
2
3

1
a
test

10
11
12