CSharp - Statement foreach loops

Introduction

foreach statement iterates over each element in an enumerable object.

Both an array and a string are enumerable.

The following code enumerates over the characters in a string, from the first character through to the last:

Demo

using System;
class MainClass/*from   w  w w .  j av a  2s  .  co m*/
{
   public static void Main(string[] args)
   {
      foreach (char c in "book2s.com")   // c is the iteration variable
          Console.WriteLine (c);
   }
}

Result

Related Topics