Foreach loop in C#

Now we are going to learn about the foreach loop in C# .
  • 3157

Introduction

The foreach loop in C# construct an array which executes the element . It collect the items and does not use an index. The working method of the foreach loop is very simple. It collect the elements and returns each elements in order. It is also called enumeration. The foreach statements is used to iterate through the collection to get the desired information. The foreach loop have a advantage that it should not change the contents of the collection and avoid the unpredictable side effects. The foreach loop have a new feature is that we can break out the loop by the use of break button.

Example of the foreach loop

class Program
{
  static
void Main(string[] args)
    {
      int
[] array = new int[] { 0, 1, 2, 3, 5, 8, 13 };
         foreach
(int i in array)
       {
            System.Console.WriteLine(i);
                System.Console.ReadLine();
        }
     }
}

Output

foreach.jpg

 

© 2020 DotNetHeaven. All rights reserved.