Maha

Maha

  • NA
  • 0
  • 326k

for instead of foreach

Feb 18 2012 4:39 PM
Q(10) of the chapter
I wish to know in this program whether it is possible to use FOR instead FOREACH.

using System;
namespace _6e10
{
class Program
{
static void Main(string[] args)
{
int one = 1, three = 3, five = 5;
int[] num = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //Total 55

sum(one);
sum(three);
sum(five);
sum(num);

Console.ReadKey();
}
static void sum(params int[] integer)
{
int total = 0;

foreach (int number in integer)
{
total = total + number;
}

Console.WriteLine("Total {0}", total);
}
}
}


Answers (3)