How to use Foreach Loop in c#

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

Introduction

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.

Example

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 )

            {

                Console.WriteLine(number);

            }

            Console.ReadLine();

        }

}

 

The output of following program


 Clipboard109.jpg

© 2019 DotNetHeaven. All rights reserved.