Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

foreach Loop

Date- Dec 09,2023

3089

The foreach loop in C# executes a block of code on each element in an array or a collection of items. When executing foreach loop it traversing items in a collection or an array. The foreach loop is useful for traversing each item in an array or a collection of items and displayed one by one. the foreach loop when working with arrays and collections to iterate through the items of arrays/collections. The foreach loop iterates through each item hence called foreach loop.

foreach(variable type in collection){
  // code block
}

The in keyword used along with foreach loop is used to iterate over the iterable-item. The in keyword selects an item from the iterable-item on each iteration and store it in the variable element. On first iteration, the first item of iterable-item is stored in element. On second iteration, the second element is selected and so on. The number of times the foreach loop will execute is equal to the number of elements in the array or collection. Here is an example of iterating through an array using the for loop:

Example 1: Printing array using foreach loop

using System;
public class Program
{
	public static void Main()
	{
	 Console.WriteLine("foreach loop Example!");  
         string[] name = new string[5] { "Shubham","Rahul","Mohit","Anmol","Sham"};  
         foreach (var num in name)  
         {  
            Console.WriteLine("Name - "+ num);  
         }  
	}
}

Output :

foreach loop Example!
Name - Shubham
Name - Rahul
Name - Mohit
Name - Anmol
Name - Sham

Example 2: Printing array using foreach loop

using System;
public class Program
{
      public static void Main()
      {
	Console.WriteLine("foreach loop Example!");  
        string[] name = new string[] { "Shubham Batra"};  
        foreach (var num in name)  
        {  
	   if(num =="Shubham Batra")
	   {
             Console.WriteLine("Hey - "+ num);  
	   }
        }  
     }
}

Output

foreach loop Example!
Hey - Shubham Batra

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers