Topic : Params Arrays | Parameter Arrays | in C# Programming Languages: | Params Keyword and its Use:



Params Arrays:

Sometimes, we are not assured about the member of Parameters or we want to create a Method that can accept a Number of Parameters at Runtime. C# Param Arrays (Or Parameter Arrays) come to help in such situation. The Params Keyword create an Array at Runtime that receives and holds "N" number of Parameters.

Params keyword in C# allows a method or a constructor to receive a variable number of parameters. When params are used then the compiler converts the arguments passed to a method into a temporary array. This temporary array than can be used in the receiving method to perform some operation on its elements. When using params you can pass a comma-separated list of arguments of the type specified in the method signature or an array of arguments of the specified type. It also gives us the flexibility to send no arguments to the method and in such a case the length of the params list is zero.



Syntax of Params Parameter:

By Using the Params keyword, we can specify a Method parameter that takes a Variable Number of Arguments using this Syntax:


  Access-Specifier   Data-Type    Variable-name( paramsint[]  Variable-name )
    {
     //Body:
   }


  • In Above Syntax:

    » Access-Specifier is the scope of Accessibility of this Method. could be public, private or protected.
    » Data-Type could be any valid Data type like int or char.
    » params is the keyword in C# to specify a Method parameter that takes a variable number of Arguments.
    » int[] is an param array.
    » and the Variable-Name is the variable that can holds "n" number of parameters at run time because it is declared with params keyword.



Example:

Let's have an simple example to know how we can declare and use Params Arrays.in this example, we will create a Method add() using Params keyword, that will receives any number of Integer parameters at run time and Returns the Sum of all Those Numbers.


/*Example: Params Array: InfoBrother:*/

using System;

namespace paramsArray
{
    class Program
    {
     
        static int add(params int[] numbers)
        {
            int sum = 0;
            foreach (int n in numbers)
            {
                sum = sum + n;
            }
            return sum;
        }

        //main method:
        static void Main(string[] args)
        {
            int sum;

            // passing 05  parameters
            sum = Program.add(1, 2, 3, 4, 5);
            Console.WriteLine("Sum of 1,2,3,4,5 is:\t{0}", sum);

            // passing 07 parameters
            sum = Program.add(10, 20, 30, 40, 50, 60, 70);
            Console.WriteLine("Sum of 10,20,30,40,50,60,70 is:\t{0}", sum);
            Console.ReadLine();
        }
    }
}


  • There are Three Restriction of Using Params Keyword in C#:

    » We can send only comma separated list of arguments to a method.
    » No additional parameters are permitted after the params keyword in a method declaration.
    » Only one params keyword is permitted in a method declaration.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement