In this article I am going to discuss about C# Foreach Loop in detail but before that if you had not read my previous article of C# Do While Loop then must read it. It is the last type of Iteration statements in control flow statements. C# foreach loop is used when we want to iterate over the array, list or any IEnumerable thing without giving any condition.

Introduction to C# Foreach Loop

Foreach loop is used to iterate over the arrays, Lists and IEnumerable things to display it without giving any initialization , condition and any iterator ( increment/decrement). In simple words the C# foreach loop is loop that doesn’t include initialization, termination, and increment/decrement characteristics. The C# foreach loop is used to iterate over the elements of a collection and it executes the loop body for each element present in the array or collection. The C# foreach loop is come with System.Collections.Generic namespace and can be used with any collection type. The Syntax for the foreach loop is given below:

foreach(datatype variable in collection_type){
//block of statements
}

C# Program to Explain Foreach Loop

using System;
namespace ForeachLoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // creating an array of integer type
            int[] Array = new int[] { 1, 2, 3, 4, 5, 6 };

            Console.WriteLine("Print Array Elememnts using C# Foreach Loop:");
            
            // The foreach loop will run till the last element of the array
            foreach (int item in Array)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}
Output:
Print Array Elememnts using C# Foreach Loop:
1
2
3
4
5
6

In above program I have declared one array of type integer and then I am using foreach loop which is iterate over the Array and then simply printing the array value. You can see from above code that foreach loop doesn’t need any initialization and condition. The keyword in in the foreach loop is used to iterate over the collection. The in keyword selects an item from the collection on each iteration and stores it in the variable element. On the first iteration, the first item of the collection is stored in element. On the second iteration, the second element is selected, and so on.

Difference between For Loop and Foreach Loop

The C# for loop executes a block of statements as long as the given condition is true. But foreach loop executes a block of statements for each element present in the array or collection and there is no need to define any condition and initialization. In for loop it is possible to traverse over the array elements in forward and backward directions but in foreach loop it is not possible to traverse over the array in both forward and backward directions because with foreach loop we can only traverse over array in forward direction. For example with for loop we can iterate over array from index 0 to 9 and from index 9 to 0 but with foreach loop we can only iterate from index 0 to 9.

Drawbacks of Foreach Loop

  1. As we have already discussed foreach loop cannot iterate over backward direction therefore most of the times we prefer to use for loop for traversing.
  2. Foreach loop takes extra time in comparsion of foreach loop because it takes more memory as compared to for loop.
  3. The C# foreach loop does not keep track of indexes. So, it is not possible to get the array index or collection index using the Foreach loop.

When to Use Foreach Loop?

We can use foreach loop if we only have to iterate over the array, List or any other collection type. By using foreach loop our program looks readable and easy to understand. Instead of this like nested for loop, nested while loops we can also use nested foreach loop. Most of the times you will be using for loop but for smaller programs you can go with foreach loop.

Conclusion

In this C# tutorials article we have discussed last type of iteration statement foreach loop in detail. We have covered the difference between for loop and foreach loop and when to use foreach loop in our program. Hope you like this article of foreach loop. Now you try creating programs to understand more deeply about the control flow statements. In my next article I am going to discuss about the jump statements in very detail.