C#關鍵字—方法參數

來源:互聯網
上載者:User

標籤:否則   new   ++   spl   lang   ref   you   more   方法參數   

Printed From Microsoft.docs;

使用 params 關鍵字可以指定採用數目可變的參數的方法參數。

可以發送參數聲明中所指定類型的逗號分隔的參數列表或指定類型的參數數組。 還可以不發送參數。 如果未發送任何參數,則 params 列表的長度為零。

在方法聲明中的 params 關鍵字之後不允許有任何其他參數,並且在方法聲明中只允許有一個 params 關鍵字。

public class MyClass{    public static void UseParams(params int[] list)    {        for (int i = 0; i < list.Length; i++)        {            Console.Write(list[i] + " ");        }        Console.WriteLine();    }    public static void UseParams2(params object[] list)    {        for (int i = 0; i < list.Length; i++)        {            Console.Write(list[i] + " ");        }        Console.WriteLine();    }    static void Main()    {        // You can send a comma-separated list of arguments of the         // specified type.        UseParams(1, 2, 3, 4);        UseParams2(1, ‘a‘, "test");        // A params parameter accepts zero or more arguments.        // The following calling statement displays only a blank line.        UseParams2();        // An array argument can be passed, as long as the array        // type matches the parameter type of the method being called.        int[] myIntArray = { 5, 6, 7, 8, 9 };        UseParams(myIntArray);        object[] myObjArray = { 2, ‘b‘, "test", "again" };        UseParams2(myObjArray);        // The following call causes a compiler error because the object        // array cannot be converted into an integer array.        //UseParams(myObjArray);        // The following call does not cause an error, but the entire         // integer array becomes the first element of the params array.        UseParams2(myIntArray);    }}/*Output:    1 2 3 4    1 a test               5 6 7 8 9    2 b test again    System.Int32[]*/

 

namespace _050_參數數組__定義一個參數個數不確定的函數_ {    class Program {        static int Sum(int[] array)//如果一個函數定義了參數,那麼在調用這個函數的時候,一定要傳遞對應類型的參數,否則無法調用(編譯器編譯不通過)        {            int sum = 0;            for (int i = 0; i < array.Length; i++)            {                sum += array[i];            }            return sum;        }        static int Plus(params int[] array)//這裡定義了一個int類型的參數數組,參數數組和數組參數(上面的)的不同,在於函數的調用,調用參數數組的函數的時候,我們可以傳遞過來任意多個參數,然後編譯器會幫我們自動組拼成一個數組,參數如果是上面的數組參數,那麼這個數組我們自己去手動建立        {            int sum = 0;            for (int i = 0; i < array.Length; i++) {                sum += array[i];            }            return sum;        }        static void Main(string[] args)        {            int sum = Sum(new int[] {23, 4, 34, 32, 32, 42, 4});            Console.WriteLine(sum);            int sum2 = Plus(23, 4, 5, 5, 5, 32, 423, 42, 43,23,42,3);//參數數組就是幫我們 減少了一個建立數組的過程             Console.WriteLine(sum2);            Console.ReadKey();        }    }}

 

C#關鍵字—方法參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.