C# Params

In C#, params is a keyword which is used to specify a parameter that takes variable number of arguments. It is useful when we don't know the number of arguments prior. Only one params keyword is allowed and no additional parameter is permitted after params keyword in a function declaration.

Example #1
snippet
using System;
namespace AccessSpecifiers
{
    class Program
    {
        // User defined function
        public void Show(params int[] val) // Params Paramater
        {
            for (int i=0; i
Output
2 4 6 8 10 12 14
Example #2

In this example, we are using object type params that allow entering any number of inputs of any type.

snippet
using System;
namespace AccessSpecifiers
{
    class Program
    {
        // User defined function
        public void Show(params object[] items) // Params Paramater
        {
            for (int i = 0; i < items.Length; i++)
            {
                Console.WriteLine(items[i]);
            }   
        }
        // Main function, execution entry point of the program
        static void Main(string[] args)
        {
            Program program = new Program(); // Creating Object
            program.Show("Ramakrishnan Ayyer","Ramesh",101, 20.50,"Peter", 'A'); // Passing arguments of variable length
        }	
    }
}
Output
Ramakrishnan Ayyer Ramesh 101 20.5 Peter A
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +