Passing parameter by param

The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

 

Params parameter is a very useful feature in C#. It is used when we don't know the number of parameters will be passed to the called method.

 

Param can accept multiple values or "params" should be a single dimensional or a jagged array.

 

Practical demonstration of passing parameter by param

 

using System;

 

namespace param_parameter

{

    class Program

    {

        class XX

        {

            public void print(params int[] numbers)

            {

                foreach(int x in numbers)

                {

                    Console.WriteLine(" " + x);

                }

            }

        }

        static void Main(string[] args)

        {

 

            int[] numbers = { 1, 2, 3, 4, 5, 6 };

            int a = 10, b = 20, c = 30, d = 40;

 

            XX obj = new XX();

 

            obj.print(a, b, c, d);

            obj.print(numbers);

 

            Console.ReadLine();

        }

    }

}

 

One interesting aspect about params is that it will be invoked only if no other overloaded methods apply.

 

See the below program to have explanation of above sentence.

 

using System;

 

namespace Param_example1

{

    class Program

    {

        public class ParamClass

        {

            public ParamClass()

            {

                Console.WriteLine("Default Constructor with no parameters");

            }

            public ParamClass(string a)

            {

                Console.WriteLine("Constructor with single parameter - {0}", a);

            }

            public ParamClass(string a, string b)

            {

                Console.WriteLine("Constructor with two parameters - {0} \t {1}", a, b);

            }

 

            public ParamClass(params string[] p)

            {

                int count = p.Length;

 

                for (int i = 0; i < count; i++)

                {

                    Console.Write(p[i] + "\t");

                }

            }

        }

        static void Main(string[] args)

        {

            ParamClass ParamClass = new ParamClass();

            Console.WriteLine();

            ParamClass = new ParamClass("Rocky");

            Console.WriteLine();

            ParamClass = new ParamClass("Rocky", "Sam");

            Console.WriteLine();

            ParamClass = new ParamClass("Rocky", "Sam", "Tina", "Ana", "Omar", "John");

            Console.ReadLine();

        }

    }

}

 

In the above program, there are four overloaded constructors, including a default constructor and a single params constructor.

 

The params constructor is invoked only in the final line when none of the other overloaded constructors apply.

 

Important point in Param parameters 

  • Only one params keyword is permitted in a method declaration.
  • Param parameter should be the last parameters in actual parameter list.

Hope now your would have been clear with the ways of parameter passing using param keyword. If you have any feedback or query, please mail me.


Similar Articles