C# Foreach Loop with Examples

In c#, Foreach loop is used to loop through an each item in array or collection object to execute the block of statements repeatedly.

 

Generally, in c# Foreach loop will work with the collection objects such as arrays, list, etc. to execute the block of statements for each element in the array or collection.

 

After completion of iterating through an each element in collection, control is transferred to the next statement following the foreach block.

 

In c#, we can use break, continue, goto and return statements within foreach loop to exit or continue to the next iteration of the loop based on our requirements.

Syntax of C# Foreach Loop

Following is the syntax of defining the Foreach loop in c# programming language.

 

foreach (Type var_name in Collection_Object) {

 

// Statements to Execute

 

}

If you observe above syntax, we defined a foreach loop with collection object and required variable name to access an elements from the collection object.

 

Here Type is a built in data-type or custom class type and var_name is a variable name to access an elements from collection object (Collection_Object) to use it in body of the foreach loop.

C# Foreach Loop Flow Chart

Following is the pictorial representation of foreach loop process flow diagram in c# programming language.

 

C# Foreach Loop Flow Chart Diagram

 

Now we will see how to use foreach loop in c# programming language with examples.

C# Foreach Loop with Array Example

Following is the example of using foreach loop in c# programming language to iterate or loop through an array elements.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] names = new string[3] { "Suresh Dasari", "Rohini Alavala", "Trishika Dasari" };

            foreach (string name in names)

            {

                Console.WriteLine(name);

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we created a string array object “names” and looping through an each element of array object using foreach loop and assigning an array elements to string variable “name”.

 

To know more about arrays in c# programming language, check this c# arrays with examples.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Foreach Loop with Arrays Example Result

 

If you observe above result, we loop through an each element of array and printed those values on console window based on our requirements.

C# Foreach Loop with List Example

Same as c# foreach with arrays, we can use foreach loop with list object to process each element in the list object but inside of foreach loop it won’t allow us to modify (add or delete) the list object items.

 

To learn more about lists in c# programming, check this c# lists with examples.

 

Following is the example of using foreach loop in c# programming language to iterate or loop through a list elements.

 

using System;

using System.Collections.Generic;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            List<string> names = new List<string>() { "Suresh Dasari", "Rohini Alavala", "Trishika Dasari" };

            foreach (string name in names)

            {

                Console.WriteLine(name);

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used System.Collections.Generic namespace to access the List object and adding string elements to the list. We used foreach to loop through an items in the list to print it on console window.

Output of C# Foreach Loop with List Example

When we run above c# example, we will get the result like as shown below.

 

C# Foreach Loop with List Example Result

 

This is how we can use foreach loop in c# programming language to loop through an each element in the array or collection objects based on our requirements.