C# foreach loop

C# foreach loop C#-ForEach-Loop

C# foreach loop was first introduced in dot net programming languages. Later it's copied in other programming languages like Java. Here we will see example of ForEach—Loop in C# and VB.net program.

C# foreach loop example

using System;
namespace Csharp.FOREACH.Loop.Example
{
class Program
{
static void Main(string[] args)
{
string[] arrayOfStrings = new string[] { "Asp.net", "C#.net", "VB.net", "Jquery" };
foreach (string str in arrayOfStrings)
{
Console.WriteLine("Element is: " + str);
}
Console.ReadLine();
}
}
}
Output
Element is: Asp.net
Element is: C#.net
Element is: VB.net
Element is: Jquery
VB.net-Program

(VB.net) ForEach Loop

Namespace Csharp.FOREACH.Loop.Example
Class Program
Private Shared Sub Main(args As String())
Dim arrayOfStrings As String() = New String() {"Asp.net", "C#.net", "VB.net", "Jquery"}
For Each str As String In arrayOfStrings
Console.WriteLine("Element is: " & str)
Next
Console.ReadLine()
End Sub
End Class
End Namespace
C#-For-Loop-break

c# foreach loop break

 class Program
{
static void Main(string[] args)
{
string[] arrayOfStrings = new string[] { "Asp.net", "C#.net", "VB.net", "Jquery" };
foreach (string str in arrayOfStrings)
{
if (str == "VB.net")
break;
Console.WriteLine("Element is: " + str);
}
Console.ReadLine();
}
}
Element is: Asp.net
Element is: C#.net

(VB.net) ForEach loop break example

Class Program
Private Shared Sub Main(args As String())
Dim arrayOfStrings As String() = New String() {"Asp.net", "C#.net", "VB.net", "Jquery"}
For Each str As String In arrayOfStrings
If str = "VB.net" Then
Exit For
End If
Console.WriteLine("Element is: " & str)
Next
Console.ReadLine()
End Sub
End Class
Element is: Asp.net
Element is: C#.net

C# ForEach loop Continue example

using System;
namespace Csharp.FOREACH.Loop.Example
{
class Program
{
static void Main(string[] args)
{
string[] arrayOfStrings = new string[] { "Asp.net", "C#.net", "VB.net", "Jquery" };
foreach (string str in arrayOfStrings)
{
if (str == "VB.net")
continue;
Console.WriteLine("Element is: " + str);
}
Console.ReadLine();
}
}
}
Element is: Asp.net
Element is: C#.net
Element is: Jquery

(VB.net) ForEach loop continue example

Namespace Csharp.FOREACH.Loop.Example
Class Program
Private Shared Sub Main(args As String())
Dim arrayOfStrings As String() = New String() {"Asp.net", "C#.net", "VB.net", "Jquery"}
For Each str As String In arrayOfStrings
If str = "VB.net" Then
Continue For
End If
Console.WriteLine("Element is: " & str)
Next
Console.ReadLine()
End Sub
End Class
End Namespace

c# foreach loop count

C# foreach loop count the number of iteration.

using System;
namespace Csharp.FOREACH.Loop.Example
{
class Program
{
static void Main(string[] args)
{
string[] arrayOfStrings = new string[] { "Asp.net", "C#.net", "VB.net", "Jquery" };
int counter = 0;
foreach (string str in arrayOfStrings)
{              
Console.WriteLine("Element is: " + str);
counter++;
}
Console.WriteLine("Total count: "+ counter);
Console.ReadLine();
}
}
}
Element is: Asp.net
Element is: C#.net
Element is: VB.net
Element is: Jquery
Total count: 4

Tags:

C# looping through arrayC# For LoopC# while loopC# Do While Loop ExampleJavaScript For LoopC# Listbox ForeachC# Break StatementC# Arraylist foreachc# arraylistC# StopWatchC# Regular Expression Get Text Between Brackets

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.