C#基础之方法参数

params

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

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

 1 public class MyClass 
 2 {
 3 
 4     public static void UseParams(params int[] list) 
 5     {
 6         for (int i = 0 ; i < list.Length; i++)
 7         {
 8             Console.Write(list[i] + " ");
 9         }
10         Console.WriteLine();
11     }
12 
13     public static void UseParams2(params object[] list) 
14     {
15         for (int i = 0 ; i < list.Length; i++)
16         {
17             Console.Write(list[i] + " ");
18         }
19         Console.WriteLine();
20     }
21 
22     static void Main() 
23     {
24         UseParams(1, 2, 3);
25         UseParams2(1, 'a', "test"); 
26 
27         // An array of objects can also be passed, as long as
28         // the array type matches the method being called.
29         int[] myarray = new int[3] {10,11,12};
30         UseParams(myarray);
31     }
32 }
33 /*
34 Output:
35     1 2 3
36     1 a test
37     10 11 12
38 */
示例代码
out

  out 关键字会导致参数通过引用来传递。这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化。若要使用 out 参数,方法定义和调用方法都必须显式使用out 关键字

 1 class OutExample
 2 {
 3     static void Method(out int i)
 4     {
 5         i = 44;
 6     }
 7     static void Main()
 8     {
 9         int value;
10         Method(out value);
11         // value is now 44
12     }
13 }
示例代码

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

ref 和 out 关键字在运行时的处理方式不同,但在编译时的处理方式相同。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:

1 class CS0663_Example
2 {
3     // Compiler error CS0663: "Cannot define overloaded 
4     // methods that differ only on ref and out".
5     public void SampleMethod(out int i) { }
6     public void SampleMethod(ref int i) { }
7 }

  但是,如果一个方法采用 ref 或 out 参数,而另一个方法不采用这两类参数,则可以进行重载,如下所示:

1 class OutOverloadExample
2 {
3     public void SampleMethod(int i) { }
4     public void SampleMethod(out int i) { i = 5; }
5 }

  属性不是变量,因此不能作为 out 参数传递。

  当希望方法返回多个值时,声明 out 方法很有用。使用 out 参数的方法仍然可以将变量作为返回类型来访问(请参见 return),但它还可以将一个或多个对象作为 out 参数返回给调用方法。此示例使用 out 在一个方法调用中返回三个变量。请注意,第三个参数所赋的值为 Null。这样使方法可以有选择地返回值。

 1 class OutReturnExample
 2 {
 3     static void Method(out int i, out string s1, out string s2)
 4     {
 5         i = 44;
 6         s1 = "I've been returned";
 7         s2 = null;
 8     }
 9     static void Main()
10     {
11         int value;
12         string str1, str2;
13         Method(out value, out str1, out str2);
14         // value is now 44
15         // str1 is now "I've been returned"
16         // str2 is (still) null;
17     }
18 }
示例代码
ref

  ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字

 1 class RefExample
 2 {
 3     static void Method(ref int i)
 4     {
 5         i = 44;
 6     }
 7     static void Main()
 8     {
 9         int val = 0;
10         Method(ref val);
11         // val is now 44
12     }
13 }

  按引用传递值类型(如本主题前面所示)是有用的,但是 ref 对于传递引用类型也是很有用的。这允许被调用的方法修改该引用所引用的对象,因为引用本身是按引用来传递的。下面的示例显示出当引用类型作为 ref 参数传递时,可以更改对象本身。

 1 class RefExample2
 2 {
 3     static void Method(ref string s)
 4     {
 5         s = "changed";
 6     }
 7     static void Main()
 8     {
 9         string str = "original";
10         Method(ref str);
11         Console.WriteLine(str);
12     }
13 }
14 // Output: changed
示例代码

 

posted @ 2013-10-15 11:44  DoNetCoder  阅读(284)  评论(0编辑  收藏  举报