The params keyword specifies a variable number of arguments to be passed to a constructor or method, such as:

public Test( string arg0, float arg1, params int[] args )
{
}

The params keyword defines an optional array of a variable number of arguments (parameters).  There can be only one argument with the params keyword, and it must appear at the end of the argument list.  Also, the params keyword must describe a single-dimension array.

One interesting aspect about params is that it will be invoked only if no other overloaded methods apply.  In the example console program below, there are five overloaded constructors, including a default constructor and a single params constructor: 

using System;

namespace MultipleParameters
{
    public class Test
    {
        public Test()
        {
            Console.WriteLine( "default" );
        }
        public Test( string arg0 )
        {
            Console.WriteLine( "one: arg0={0}", arg0 );
        }
        public Test( string arg0, string arg1 )
        {
            Console.WriteLine( "two: arg0={0}, arg1={1}", arg0, arg1 );
        }
        public Test( string arg0, string arg1, string arg2 )
        {
            Console.WriteLine( "three: arg0={0}, arg1={1}, arg2={2}",
                arg0, arg1, arg2 );
        }
        public Test( params string[] args )
        {
            int count = args.Length;
            Console.Write( "params: " );
            if (count == 0)
                Console.Write( "(no args)" );
            else
            {
                for (int i = 0; i < count; i++)
                {
                    string prefix = i == 0 ?
                        null : ", ";
                    Console.Write( "{0}arg{1}={2}", prefix, i+1, args[i] );
                }
            }
            Console.WriteLine();
        }
    }
    class Program
    {
        static void Main( string[] args )
        {
            Test test = new Test();
            test = new Test( "hello" );
            test = new Test( "hello", "goodbye" );
            test = new Test( "hello", "goodbye", "mahalo" );
            test = new Test( "hello", "goodbye", "mahalo", "bonjour" );
            Console.ReadLine();
        }
    }
}

Here is the console program output.  Notice how the default constructor is invoked in the first line, and the params constructor is invoked only in the final line when none of the other overloaded constructors apply:

default

one: arg0=hello

two: arg0=hello, arg1=goodbye

three: arg0=hello, arg1=goodbye, arg2=mahalo

params: arg1=hello, arg2=goodbye, arg3=mahalo, arg4=bonjour

Here is the output if we comment out the default constructor.  Notice how the params constructor is invoked in the first line when no arguments are specified.  In that case, an empty string array is passed to the params constructor:

params: (no args)

one: arg0=hello

two: arg0=hello, arg1=goodbye

three: arg0=hello, arg1=goodbye, arg2=mahalo

params: arg1=hello, arg2=goodbye, arg3=mahalo, arg4=bonjour

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot