How do I exit a foreach loop

  • Thread starter Thread starter Ray Stevens
  • Start date Start date
R

Ray Stevens

What is the command to exit a foreach loop prior to it's natural termination
(such as finding a specific string in an array)?
 
Hi Ray,

In addition to the forementioned 'break' you can also use goto

foreach(this t of that)
{
if(condition)
goto SomeLabel;
}

SomeLabel: ;
 
I rarely use them, but they are handy when stepping out of deep loops,
redoing loops etc.

And odd thing about C# labels is that they can only be inserted in front
of a code statement, hence the ; after label:
 
Morten Wennevik said:
I rarely use them, but they are handy when stepping out of deep loops,
redoing loops etc.

I had it hammered into me so hard that goto's are evil by my professors that
I still (9 years later) get twitchy when I see them ;D

Having spent all but the last year of my professional programming career
developing mainly in VB, you'd think I wouldn't still get cold shivers every
time I think of them, but there you go. Or maybe it's because I saw too much
goto horror...
And odd thing about C# labels is that they can only be inserted in front
of a code statement, hence the ; after label:

I did not know that.

<he says, glaring balefully at recently abandoned "The C# Programming
Language">
 
Back
Top