码迷,mamicode.com
首页 > Windows程序 > 详细

C# 传不定参数

时间:2021-01-01 12:23:11      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:for   ror   ram   convert   word   doc   play   comm   isp   

 1 public class MyClass
 2 {
 3     public static void UseParams(params int[] list)
 4     {
 5         for (int i = 0; i < list.Length; i++)
 6         {
 7             Console.Write(list[i] + " ");
 8         }
 9         Console.WriteLine();
10     }
11 
12     public static void UseParams2(params object[] list)
13     {
14         for (int i = 0; i < list.Length; i++)
15         {
16             Console.Write(list[i] + " ");
17         }
18         Console.WriteLine();
19     }
20 
21     static void Main()
22     {
23         // You can send a comma-separated list of arguments of the
24         // specified type.
25         UseParams(1, 2, 3, 4);
26         UseParams2(1, a, "test");
27 
28         // A params parameter accepts zero or more arguments.
29         // The following calling statement displays only a blank line.
30         UseParams2();
31 
32         // An array argument can be passed, as long as the array
33         // type matches the parameter type of the method being called.
34         int[] myIntArray = { 5, 6, 7, 8, 9 };
35         UseParams(myIntArray);
36 
37         object[] myObjArray = { 2, b, "test", "again" };
38         UseParams2(myObjArray);
39 
40         // The following call causes a compiler error because the object
41         // array cannot be converted into an integer array.
42         //UseParams(myObjArray);
43 
44         // The following call does not cause an error, but the entire
45         // integer array becomes the first element of the params array.
46         UseParams2(myIntArray);
47     }
48 }

链接:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params

C# 传不定参数

标签:for   ror   ram   convert   word   doc   play   comm   isp   

原文地址:https://www.cnblogs.com/YangARTuan/p/14199956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!