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

Currently rated 3.2 by 17 people

  • Currently 3.176471/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5