tags:

views:

167

answers:

4

Is there a way of calling a method/lines of code multiple times not using a for/foreach/while loop?

For example, if I were to use to for loop:

int numberOfIterations = 6;
for(int i = 0; i < numberOfIterations; i++)
{
   DoSomething();
   SomeProperty = true;
}

The lines of code I'm calling don't use 'i' and in my opinion the whole loop declaration hides what I'm trying to do. This is the same for a foreach.

I was wondering if there's a looping statement I can use that looks something like:

do(6)
{
   DoSomething();
   SomeProperty = true;
}

It's really clear that I just want to execute that code 6 times and there's no noise involving index instantiating and adding 1 to some arbitrary variable.

As a learning exercise I have written a static class and method:

Do.Multiple(int iterations, Action action)

Which works but scores very highly on the pretentious scale and I'm sure my peers wouldn't approve.

I'm probably just being picky and a for loop is certainly the most recognisable, but as a learning point I was just wondering if there (cleaner) alternatives. Thanks.

(I've had a look at this thread, but it's not quite the same) http://stackoverflow.com/questions/2248985/using-ienumerable-without-foreach-loop

+11  A: 

Actually, the for loop does not hide what you're trying to do.
Anyone reading your code is already familiar with standard for loops and will understand instantly what you're doing.

SLaks
I agree 100% but I would get rid of `numberOfIterations` because it doesn't really make it any clearer.
ChaosPandion
I only used that as part of the fake example in the question. But valid point
RichK
+11  A: 

You could score even more highly on the pretension scale by making it an extension method:

public static void Times(this int iterations, Action action)
{
    for (int i = 0; i < iterations; i++)
    {
        action();
    }
}
...
6.Times(() => {
    DoSomething();
    SomeProperty = true;
});

But I would definitely stick with a for loop. It's the idiomatic, well-recognised way of doing it.

Jon Skeet
Love it... have to find an excuse to use that sometime :)
slugster
Really nice. It's almost tempting :p
RichK
This is absolutely the way to go. We have made it a standard on my team.
DRBlaise
A: 

How about a do....until or if...then

Use a counter inside the loop and have it loop UNTIL the counter reaches 6.

Or an if-then statement with a counter. If your value is 6, break out of the loop.

I'm not a programming guru, but if you have an action that needs to be done a set number of times, there would be a need for a counter somewhere, whether it's a function of your loop or if it has to be created with a counter.

tahdhaze09
+2  A: 

Not a loop in sight

private MethodDelegate MultiMethod(MethodDelegate m, int count) {
  MethodDelegate a;
  if (count > 0) {
    a = m;
    a += MultiMethod(m, --count);
  } else {
    a = delegate { };
  }
  return a;
}

and you get a great syntax for invocation!

MultiMethod(action, 99)();
AlanN