zoukankan      html  css  js  c++  java
  • C# 传不定参数

     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

  • 相关阅读:
    泛型总结
    Java多线程(学习篇)
    Java线程:总结
    Java线程:线程交互
    Java线程:线程安全类和Callable与Future(有返回值的线程)
    Java线程:条件变量、原子量、线程池等
    Java线程:堵塞队列与堵塞栈
    Java线程:锁
    poj 1679 The Unique MST(唯一的最小生成树)
    poj 1659 Frogs' Neighborhood (DFS)
  • 原文地址:https://www.cnblogs.com/YangARTuan/p/14199956.html
Copyright © 2011-2022 走看看