如果在为方法声明参数时未使用 ref 或 out,则该参数可以具有关联的值。可以在方法中更改该值,但当控制传递回调用过程时,不会保留更改的值。通过使用方法参数关键字,可以更改这种行为。

params

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

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

示例

字面意思比较难懂,所以看示例很有用。

// keywords_params.cs

using System;

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

    }


    
static void Main()
    
{
        
// 一般做法是先构造一个对象数组,然后将此数组作为方法的参数
        object[] arr = new object[3100'a'"keywords" };
        UseParams(arr);

        
// 而使用了params修饰方法参数后,我们可以直接使用一组对象作为参数
        
// 当然这组参数需要符合调用的方法对参数的要求
        UseParams(100'a'"keywords");

        Console.Read();
    }

}

ref

ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。

  1. 若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。
  2. 传递到 ref 参数的参数必须最先初始化。这与 out 不同,out 的参数在传递之前不需要显式初始化。
  3. 属性不是变量,因此不能作为 ref 参数传递。
  4. 尽管 ref 和 out 在运行时的处理方式不同,但它们在编译时的处理方式是相同的。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的。如果尝试这么做,将导致不能编译该代码。
  5. 如果一个方法采用 ref 或 out 参数,而另一个方法不采用这两类参数,则可以进行重载。

示例

按引用传递值类型是有用的,但是 ref 对于传递引用类型也是很有用的。这允许被调用的方法修改该引用所引用的对象,因为引用本身是按引用来传递的。

// keywords_ref.cs

using System;

class App
{
    
public static void UseRef(ref int i)
    
{
        i 
+= 100;
        Console.WriteLine(
"i = {0}", i);
    }


    
static void Main()
    
{
        
int i = 10;

        
// 查看调用方法之前的值
        Console.WriteLine("Before the method calling: i = {0}", i);

        UseRef(
ref i);

        
// 查看调用方法之后的值
        Console.WriteLine("After the method calling: i = {0}", i);
        Console.Read();
    }

}


/*
控制台输出:
Before the method calling : i = 10
i = 110
After the method calling: i = 110
*/

out

out 关键字会导致参数通过引用来传递。这与 ref 关键字类似。

与 ref 的不同之处:

  1. ref 要求变量必须在传递之前进行初始化。
  2. 尽管作为 out 参数传递的变量不需要在传递之前进行初始化,但需要调用方法以便在方法返回之前赋值。

示例

与 ref 示例不同的地方只要将 ref 改为 out,然后变量 i 仅需要声明即可。

static void Main()
{
    
//int i = 10; 改为
    int i;
    
//
}

 

==================

 

ref是传递参数的地址,out是返回值,两者有一定的相同之处,不过也有不同点。

  使用ref前必须对变量赋值,out不用。

  out的函数会清空变量,即使变量已经赋值也不行,退出函数时所有out引用的变量都要赋值,ref引用的可以修改,也可以不修改。

  区别可以参看下面的代码:

using System;
class TestApp
{
 static void outTest(out int x, out int y)
 {//离开这个函数前,必须对x和y赋值,否则会报错。
  //y = x;
  //上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
  x = 1;
  y = 2;
 }
 static void refTest(ref int x, ref int y)
 {
  x = 1;
  y = x;
 }
 public static void Main()
 {
  //out test
  int a,b;
  //out使用前,变量可以不赋值
  outTest(out a, out b);
  Console.WriteLine("a={0};b={1}",a,b);
  int c=11,d=22;
  outTest(out c, out d);
  Console.WriteLine("c={0};d={1}",c,d);

  //ref test
  int m,n;
  //refTest(ref m, ref n);
  //上面这行会出错,ref使用前,变量必须赋值

  int o=11,p=22;
  refTest(ref o, ref p);
  Console.WriteLine("o={0};p={1}",o,p);
 }
}

 

class Program
    {
        private static int TestOut(out int a, out int b)
        {
            a = 1;
            b = 2;
            return 222;
        }

        private static void TestRef(ref int a, ref int b)
        {
            a = 555;
            b = a;
        }

        static void Main(string[] args)
        {
            int a, b;
            int intTestOut = TestOut(out a, out b);
            Console.WriteLine("{0}, {1}", a, b);
            Console.WriteLine("{0}", intTestOut);

            a = 22;
            b = 33;
            intTestOut = TestOut(out a, out b);
            Console.WriteLine("{0}, {1}", a, b);
            Console.WriteLine("{0}", intTestOut);

            int c, d;
            c = 44;
            d = 55;
            TestRef(ref c, ref d);
            Console.WriteLine("{0}, {1}", c, d);

            Console.ReadLine();
        }
    }

posted on 2009-02-23 20:54  SamZhang  阅读(932)  评论(0编辑  收藏  举报