c# params用法

params 构造函数声明数组 而不知道数组长度 用的
在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。

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

 

posted @ 2015-12-18 22:09  翻滚吧炒鸡蛋  阅读(154)  评论(0)    收藏  举报