Pass varied number of parameter to a method - CSharp Custom Type

CSharp examples for Custom Type:Method Parameter

Description

Pass varied number of parameter to a method

Demo Code

using System;//from w w w.  j a  v  a 2  s .c  o  m
class ParamArray {
   public int AddElements(params int[] arr) {
      int sum = 0;
      foreach (int i in arr) {
         sum += i;
      }
      return sum;
   }
}
class TestClass {
   static void Main(string[] args) {
      ParamArray app = new ParamArray();
      int sum = app.AddElements(512, 720, 250, 567, 889);
      Console.WriteLine("The sum is: {0}", sum);
   }
}

Result


Related Tutorials