Explain foreach loop in C#

C# foreach circle: foreach is an exceptional kind of circle utilized in C#, which is utilized to get to the components of a cluster or assortment, as indicated by its name it can get to every component of an exhibitor assortment.

There are three kinds of circling explanations in C and C++ programming dialects: for, while and do while. C# presents another circling proclamation that is known as foreach circle.

foreach is an extraordinary sort of circle utilized in C#, which is utilized to get to the components of an exhibitor assortment, as indicated by its name it can get to every component of a cluster or assortment.

Syntax:

foreach (var item in collection)
{
	//Statements               
}

Example:

foreach (int X in ARR)
{
	Console.Write(X+ “ ”;);                
}

In the above example, ARR is a variety of components and X is number variables it gets to every component of cluster individually and prints them with the assistance of foreach circle.

Think about the program:

using System;

namespace arrayEx
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] ARR = { 1, 2, 3, 4, 5 };

            Console.WriteLine("Elements are:");
            foreach (int X in ARR)
            {
                Console.Write(X + " ");
            }
            Console.WriteLine();
        }
    }
}

Output:

Elements are:
1 2 3 4 5
Press any key to continue . . .

Above example exhibit of the utilization of Array class techniques. Sort() technique perform arranging, Reverse() strategy invert the components of exhibit and Copy technique is utilized to duplicate one cluster to another exhibit.

Leave a Comment

error: Alert: Content is protected!!