8
Reply

Making custom loops?

Roy S

Roy S

Mar 7 2011 5:47 PM
1.7k
I have a certain nesting of loops, something similar to this:

for (int i = 0; i < 10; i++)
{
        for (int j = 0; j < 10; j++)
        {
                for (int k = i + j; ;k += i + j)
                {
                        doing stuff here...
                }
        }
}

Returning all over my project. the only thing changing is what I do inside the loops.

Is there a way to put the whole loop thing somewhere else and just tell what needs to be done in the loop? I was thinking of using something like this:

public void Main()
{
        CostumLoop(i =>
        {
                doing stuff here...
        });
}

public void CostumLoop(Action<int> doStuff)
{
        for (int i = 0; i < 10; i++)
        {
                for (int j = 0; j < 10; j++)
                {
                        for (int k = i + j; ;k += i + j)
                        {
                                doStuff(k);
                        }
                }
        }
}

But then I can't use any breaks or continue etc (which I actually need)... Is there maybe a way of telling that the break in de lambda is going to executed inside a loop and that it shouldn't give an error?

Any ideas?

Answers (8)