黑马程序员 C#基础_params

来源:互联网 发布:超市广播录音软件 编辑:程序博客网 时间:2024/06/06 05:43

------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------

params 关键字在方法成员的参数列表中使用,为该方法提供了参数个数可变的能力.

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

 

// cs_params.csusing 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);    }}

输出:

1231atest101112