foreach Loop | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

foreach Loop

Date- Dec 09,2023

1350

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

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
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 | 1190
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

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts