开发者

foreach, extra conditional statement

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)
    {
        lblD开发者_开发百科o.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 could use:

foreach (string gig in giggles.Take(4))
{
    //..
}

What this does is to create a new enumerator that is restricted to the first four items from giggles. Take() is one of the LINQ extension methods, so it is a little bit different than your conditional, but the result is the same.

Basically you can achieve many things only by manipulating the collection. Here's the code that you were specifically asking for:

int i = 0;
foreach (string gig in giggles.Where( x => i <= 4))
{
    lblDo.Text = gig;
    i++;
}


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.


In a situation like this, you should just refactor this into a for loop instead of a foreach loop.

foreach doesn't work based on conditionals, but rather operations once per item of an IEnumerable. for, on the other hand, is specifically designed to handle conditional statements.

I'd write this as:

string[] giggles = new string[6] { "one", "two", "three", "four", "five", "Six" };

for (int i = 0; i < 4; ++i)
{
    lblDo.Text = giggles[i];
}

If "giggles" is something that you're pulling in from elsewhere, and may have less than 4 elements, you might want:

for (int i = 0; i < giggles.Length && i < 4; ++i)
{
    lblDo.Text = giggles[i];
}


Use a for loop instead?

for(int i = 0; i < 4 || i < giggles.Count; i++)
{
    giggles[i].doSomething();
}


Try using an if block inside the loop. and break keyword to exit loop if required:

Example:

foreach (string gig in giggles)
{
    if(i >= 4)
    {
        break;
    }
    lblDo.Text = gig;
    i++;
}


It really depends on what you're trying to do. You could use Take() in this example.

string[] giggles = new string[6] { "one", "two", "three", "four", "five", "Six" };


foreach (string gig in giggles.Take(4))
{
    lblDo.Text = gig;

}

But if you wanted for example the strings that start with "f" you might want to use the where

    foreach (string gig in giggles.Where(s => s.StartsWith("f")  ))
        {
            Console.WriteLine(gig);

        }


If fact, the foreach loop can work with extra conditionals. The Where functions can contain conditions that do not affect the list itself:

For your need, you can use something like that:

int i = 0;

foreach (var gig in giggles.Where( p => i < 4 ))
{
    ...
    i++;
}

Recently, I used it this way in order to break the loop on outside conditions:

bool saveOk = true;

foreach ( var item in myList.Where( p=> saveOk ))
{
    saveOk = saveTheItem(item);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜