C# - Break or Exit For Each Loop in VB.NET | Exit/Break For Loop in C#, VB.NET


C# - Break or Exit For Each Loop in VB.NET | Exit/Break For Loop in C#, VB.NET

Mar 26, 2013
Introduction

Here I will explain how break or exit for each loop in C#, VB.NET or Exit or break for loop in C#VB.NET

Description
   
If you want to exit or break from for each loop we need use break statement for that we need write the code like as shown below

C# Code:

for loop example

for (int i = 0; i < 10; i++)
{
int j = 4;
if (j == i)
{
break;
}
}
for each loop example

string listingId = string.Empty;
foreach (GridViewRow gvRow in grdSearchResults.Rows)
{
if (gvRow.Cells[2].Text != "0")
{
listingId = gvRow.Cells[2].Text;
}
if (!string.IsNullOrEmpty(listingId))
{
break;
}
}
VB Code:

for loop example

For i As Integer = 0 To 9
Dim j As Integer = 4
If j = i Then
Exit For
End If
Next
for each loop example

Dim listingId As String = String.Empty
For Each gvRow As GridViewRow In grdSearchResults.Rows
If gvRow.Cells(2).Text <> "0" Then
listingId = gvRow.Cells(2).Text
End If
If Not String.IsNullOrEmpty(listingId) Then
Exit For
End If
Next

Comments

Post a Comment