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   ww  w  .  ja  v a  2s  . c om
{
   public static void Main(string[] args)
   {
      foreach (char c in "book2s.com")   // c is the iteration variable
          Console.WriteLine (c);
   }
}

Result

Related Topics