Mega Code Archive

 
Categories / C# / Language Basics
 

Use the foreach loop

/* C#: The Complete Reference  by Herbert Schildt  Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use the foreach loop.   using System;    public class ForeachDemo {    public static void Main() {      int sum = 0;      int[] nums = new int[10];        // give nums some values      for(int i = 0; i < 10; i++)         nums[i] = i;        // use foreach to display and sum the values      foreach(int x in nums) {        Console.WriteLine("Value is: " + x);        sum += x;      }      Console.WriteLine("Summation: " + sum);    }  }