'Repeat' pattern in C# for batch processing

W

win_cpp

Hello,

I was wondering if there is something equivalent to 'Repeat' pattern
in C# where I can say,

Repeat(10) myobj.SayHi();

The expansion of this being,

for (int i = 0; i < 10; ++i)
{
myObj.SayHi();
}

This is like a counted loop in assembly programming ...

Thanks,
WinCPP
 
P

Paul Musso

Dans son message précédent, win_cpp a écrit :
Hello,

I was wondering if there is something equivalent to 'Repeat' pattern
in C# where I can say,

Repeat(10) myobj.SayHi();

The expansion of this being,

for (int i = 0; i < 10; ++i)
{
myObj.SayHi();
}

This is like a counted loop in assembly programming ...

Thanks,
WinCPP

Hi WinCPP,

C# doesn't include this type of operator.
But you can create a utility method like that :

public static void Repeat(int number, Action action)
{
for (int i = 0; i < number; i++)
{
action();
}
}

And, when you are using it, it makes :

Repeat(10,() => { myObj.SayHi(); });
 
D

Duggi

Dans son message précédent, win_cpp a écrit :










Hi WinCPP,

C# doesn't include this type of operator.
But you can create a utility method like that :

public static void Repeat(int number, Action action)
{
    for (int i = 0; i < number; i++)
    {
        action();
    }

}

And, when you are using it, it makes :

Repeat(10,() => { myObj.SayHi(); });

thats a good abstraction... to make Repeat()

.....

-Cnu
 
W

win_cpp

Hi Paul,

Thanks for the suggestion. I try writing that.

As for using for loop, I was a bit lazy to type all the for (; ;)
stuff just to make a function call for 'known' number of times. So I
was looking for syntactic sugar which can reduce the burden of typing
and thus making the code less error prone.

At the language level, I was wondering if such an inbuilt
functionality would help in case of the loops for which the count is
known. As is evident from the usage, the 'Action' would be independent
of the induction variable for the loop. Compilers typically do
induction variable analysis and then perform loop inversions if it can
be established that the code *will* execute at least once. If there
were a construct such as 'Repeat' I guess it would consierably help
optimizations.

Regards,
WinCPP
http://www.linkedin.com/in/mandards
 
J

JS

As for using for loop, I was a bit lazy to type all the for (; ;)
stuff just to make a function call for 'known' number of times. So I
was looking for syntactic sugar which can reduce the burden of typing
and thus making the code less error prone.

Others who read your code will be very familiar with the for(;;)
statement, but it might confuse them to see some special looping
construct. In my opinion, you should stick with a for loop for
readability. Don't succumb to laziness at the expense of readability.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top