How to use Foreach Loop in c#

In this article, I will explain how the foreach Loop can be used in C#.
  • 2561


Foreach loop executes a block of code on each element in a collection of items or an array. Foreach loop is useful for traversing each items in an array or a collection of items and displayed one by one.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

class ForEachExample


 static void Main(string[] args)


     int[] arrayList = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

     foreach (int number in arrayList )








The output of following program


© 2020 DotNetHeaven. All rights reserved.