Welcome :Guest

Congratulations!!!

Top 5 Contributors of the Month
Melody Anderson

 Home >> Code Snippets >> OOPS >> Post New Resource

# The 'params' modifier in C#

Posted By:Abhisek Panda       Posted Date: November 20, 2009    Points: 10    Category: OOPS    URL: http://www.dotnetspark.com

# The 'params' modifier in C#

In C# 'params' parameter allows us to create a method that may be sent to a set of identically typed arguments as a single logical parameter. To better understand this situation let us see a program that calculates the average of any number of integers.

```class ParamsUse

{

//method for calculating average

static int Average(params int[] values)
{

int sum = 0;

for(int i = 0;i < values.Length;i++)

sum + = values;

return (sum/values.Length);

}

static void Main(string[] args)
{

//pass int values in comma separated form

int average;

average = Average(34,56,78);

Console.WriteLine("The average is:-{0} ",average);

//pass an array of int

int[] data={34,56,78};

average = Average(data);

Console.WriteLine("The average of the array is:-{0} ",average);

}

}```

Output:-
The average is:- 56
The average of the array is:- 56

### Description:-

The Average() method is defined to take a parameter of array of integers. We can pass any number of integer to it to calculate the average. We can pass values to Average() in two ways as shown in the above program(pass values in comma separated form or pass values as array of integers). If you don't use the 'params' keyword in the definition of Average() then we could not pass values to it in comma separated form(It will give compilation error).