The use of Params in C

  • 2020-12-09 00:59:41
  • OfStack

This article illustrates the use of Params in C#. Share to everybody for everybody reference. The specific methods are as follows:


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();
        }
    }
}

The output results are as follows:

Hello
World

Params is useful when you don't know the number of arguments, though you can use collections like List instead, but using Params is more intuitive.

Hopefully this article has helped you with your C# programming.


Related articles: