Using the foreach statement to total integers in an array. - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Using the foreach statement to total integers in an array.

Demo Code

using System;// w w  w.  j  av  a2 s .  c o  m
class ForEachTest
{
   static void Main()
   {
      int[] array = {7, 6, 9, 10, 3, 7, 8, 1, 6, 7};
      int total = 0;
      // add each element's value to total
      foreach (int number in array)
      {
         total += number;
      }
      Console.WriteLine($"Total of array elements: {total}");
   }
}

Result


Related Tutorials