As a trivial example lets say I have the following grid and I am looking for a particular cells value. When found I no longer need to process the loops.
foreach(DataGridViewRow row in grid.Rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
if(cell.Value == myValue)
{
//Do Something useful
//break out of both foreach loops.
}
}
}
How is this done in C#. In Java I could use a label to name the outermost loop, and then break that loop, but I can’t seem to find an equivelant in C#.
What is the tersest way of accomplishing this in c#? I know I can set a boolean flag, and check it in the outer loop to break out of that one as well, but it just seems too verbose.
Thanks,
The most pleasant way is to break the second loop out into a function, like this:
goes to
Of course, feel free to simplify this with LINQ or whatever (you could put
CheckWhateverinto the loop condition, too.) This is just a verbose demonstration of the principle.