foreach loops - CSharp Language Basics

CSharp examples for Language Basics:for each

Introduction

The foreach statement iterates over each element in an enumerable object.

For example, both an array and a string are enumerable.

Here is an example of enumerating over the characters in a string, from the first character through to the last:

Demo Code

using System;/*from   w  ww  .  j ava 2 s.  c  o m*/
class Test
{
   static void Main(){
      foreach (char c in "asdf")   // c is the iteration variable
         Console.WriteLine (c);
      }
}

Result


Related Tutorials