Using a variable number of arguments via the params Keyword - CSharp Custom Type

CSharp examples for Custom Type:Method Parameter

Description

Using a variable number of arguments via the params Keyword

Demo Code

using System;/*from w w  w.j ava2 s  . c o  m*/
public class Util
{
   public static long Add( params int[] args )
   {
      int i = 0;
      long Total = 0;
      for( i = 0; i < args.Length; i++)
      {
         Total += args[i];
      }
      return Total;
   }
}
class MyApp
{
   public static void Main()
   {
      long Total = 0;
      Total = Util.Add( 1 );
      Console.WriteLine("Total of (1) = {0}", Total);
      Total = Util.Add( 1, 2 );
      Console.WriteLine("Total of (1, 2) = {0}", Total);
      Total = Util.Add( 1, 2, 3 );
      Console.WriteLine("Total of (1, 2, 3) = {0}", Total);
      Total = Util.Add( 1, 2, 3, 4 );
      Console.WriteLine("Total of (1, 2, 3, 4) = {0}", Total);
   }
}

Result


Related Tutorials