Advertisement
elena1234

Method with "params int[] numbers"

Dec 16th, 2020 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.56 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MethodWithParams
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             MethodSum(1, 2, 3, 4, 5, 6);
  10.             MethodSum(1, 2, 3, 4, 5, 6, 4, 7, 8, 9, 7, 5); // can invoke this method with different number of parameters
  11.         }
  12.  
  13.         static void MethodSum(params int[] numbers)
  14.         {
  15.             int sum = 0;
  16.             for (int i = 0; i < numbers.Length; i++)
  17.             {
  18.                 sum += numbers[i];
  19.             }
  20.  
  21.             Console.WriteLine(sum);
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement