471,312 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 software developers and data experts.

'Repeat' pattern in C# for batch processing

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
Sep 22 '08 #1
5 2690
Well I think you just answered your question: use a loop.

RL

win_cpp wrote:
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
Sep 22 '08 #2
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(); });

--
Paul Musso
Sep 22 '08 #3
On Sep 22, 8:10*am, Paul Musso <pa...@exakis.comwrote:
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(); });

--
Paul Musso
thats a good abstraction... to make Repeat()

.....

-Cnu
Sep 22 '08 #4
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

But you can create a utility method like that :
public static voidRepeat(int number, Action action)
{
* * for (int i = 0; i < number; i++)
* * {
* * * * action();
* * }
}
And, when you are using it, it makes :
Repeat(10,() ={ myObj.SayHi(); });
Sep 23 '08 #5
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.
Sep 23 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Christian Stork | last post: by
2 posts views Thread by Paul Reddin | last post: by
4 posts views Thread by acantatore | last post: by
1 post views Thread by Eric | last post: by
3 posts views Thread by ludwig_stuyck | last post: by
6 posts views Thread by Thomas Guettler | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.