find and solve || findandsolve.com
Please wait.....

How to use foreach loop in c#


The foreach loop is similar to the for loop with a slight difference. The foreach loop allows you to iterate through arrays and collections(collections are similar to arrays but unlike arrays,they can store different types of objects together).Unlike the for loop, the foreach loop does not check any condition. The foreach loop simply goes through the items or elements of an array of collection and executes a set of statements for every element. The foreach loop automatically terminates after the execution of statements for the last element of the array or collection.

The syntax of the foreach loop is as follow:

foreach(Data_Type  Item_Variable  in  Collection_Name)

{

   //statements

}

In the Preceding syntax,

foreach :Refers to the foreach keyword that indicates a foreach loop.

Data_Type : Refers to the data type of the items of the array or collection

Item_Variable: Refers to an item or element of the array or collection

In : Refers to the in keyword

Collection_Name: Refers to an array or collection

statements: Refers to the set of statements that is executed for every item in the array or collection.


Perform the following steps to understand the use of the while loop:
1. Start Visual Studio 2019
2.Select File->New->Project on the menu bar.The New Project dialog box opens.
3.Select Visual C#->Windows from the Installed Templates section in the left panel of the New Project dialog box
4.Select Console Application from the middle panel to create a Console application.
5.Enter the name of the application as While Loop in the Name text box and appropriate location for the application in the Location box.
6.Click the OK button to close the New Project dialog box and create the WhileLoop application.
7. Add the code snippet,shown in listing as given below to the Program.cs file:

using System;                                                                               

public class Program

{

                public static void Main()

                {

                                string[] days={"Sunday","Monday","Tuesday","Wendnesday",

                                "Thursday","Firday","Saturday"};

                                Console.WriteLine("Days of Week :");

                                foreach(string day in days)

                                {

                                       Console.WriteLine(day);

                                               

                                }

                                Console.ReadLine();

                }

}

 

We first create an array of string type, days, which the names of the days in a week. After this,we use a foreach loop to display the array elements on the colsole.In the foreach loop, the string variable, day represents a single element of the days array.

8.Press the F5 keyboard to run the applications.

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response