C#Paramsの使用

1953 ワード

using System;

namespace Params

{

    class Program

    {

        static void Main(string[] args)

        {

            PrintMany("Hello", "World");

        }

 

        static void PrintMany(params object[] allParams)

        {

            if(allParams != null)

            {

                foreach(var p in allParams)

                {

                    Console.WriteLine(p);

                }

            }

 

            Console.Read();

        }

    }

}

出力結果:
Hello

World

Paramsはパラメータの数を知らないときに役立ちます.リストなどの集合を使って代用することもできますが、Paramsを使うと直感的に見えるのではないでしょうか.
 
転載声明:本文転載先http://www.zhoumy.cn,原文リンク:http://www.zhoumy.cn/?id=6