By 22/07/2015 1 Comments

C# Foreach Loop

  • C# foreach loop is used to access or iterate each item in a collection or array.
  • A collection is a list of objects or multiple values which can be accessed or iterated by foreach loop.

Basic syntax:

foreach (data-type in collection-type)

foreach (data-type in collection-type) Declaration of foreach loop, where data-type is any type of primitive, in is a keyword which gets or extract each element from collection and assign in that primitive type, collection-types can be arrays, lists of objects, multiple values etc and that collection to be iterated by foreach loop.
{ Open foreach loop code block or scope.
Statement(s) This can be any expression or statement which to be iterated.
} Close foreach loop code block or scope.


using System;
namespace csharpBasic
    // Start class definition / declaration.
    class Program
        // Static main method void type declaration.
        static void Main(string[] args)
            //Following, a string type array is initialized with some values.
            string[] ProgramingLanguages = { "C#", "", "Javascript", "Php", "Java" };

            // Following is foreach loop declaration.        
            foreach (string displayLanguageNames in ProgramingLanguages)
            { // Starting scope of foreach loop.

                /* Iterate each elements or values from above array and 
                   print these values as in following statement.

            } // closing scope of foreach loop.


        } // End of main method definition. 
    } // End of class. 

   The Output will be:

Next Tutorial →

← Previous Tutorial

Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .