The params modifier - CSharp Custom Type

CSharp examples for Custom Type:Method Parameter

Introduction

params parameter may be on the last parameter of a method so that the method accepts any number of arguments of a particular type.

The parameter type must be declared as an array. For example:

Demo Code

using System;//from w w  w. ja  va  2 s  .c om
class Test
{
   static int Sum (params int[] ints)
   {
      int sum = 0;
      for (int i = 0; i < ints.Length; i++)
         sum += ints[i];                       // Increase sum by ints[i]
      return sum;
   }
   static void Main()
   {
      int total = Sum (1, 2, 3, 4);
      Console.WriteLine (total);              // 10
   }
}

Result

You can also supply a params argument as an ordinary array. The first line in Main is semantically equivalent to this:

int total = Sum (new int[] { 1, 2, 3, 4 } );

Related Tutorials