Sorry, hope no one marks me down for a noob question, I don’t have a C# ref book yet><
Is there a way to put an extra conditional statement inside the foreach loop, for example:
string[] giggles = new string[6] { "one", "two", "three", "four", "five", "Six" };
int i = 0;
foreach (string gig in giggles && i < 4)
{
lblDo.Text = gig;
i++;
}
This obviously doesn’t work, but is there something similar I can use or am I stuck with using an if/break statement in the loop? Thanks!
You will have to use if/break, or a for loop. The foreach loop is almost intentionally limited because the point is simply to iterate over an enumerable collection*. If you want more concise loop control, you are to use for loops, while loops or do-while loops.
*actually, anything that has a GetEnumerator() method.