The foreach loop is a different kind of looping statement in C# programming that doesn’t includes initialization, termination and increment/decrement characteristics. It uses collection or array to take value one by one and then processes them.


 foreach (string item in array)
      // statement

Where, item is a string variable that takes value from collection as array and then processes them in the body area.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            string[] arr = new string[5]; // declaring array
            //Storing value in array element
            arr[0] = "Mobile";
            arr[1] = "Laptop";
            arr[2] = "Computer";
            arr[3] = "Mouse";
            arr[4] = "KeyPad";
            arr[4] = "Camera";
            //retrieving value using foreach loop
            foreach (string name in arr)
                Console.WriteLine("Item Name is : " + name);



foreach loop in c#


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like