Proposal: Repeating action #63223
Unanswered
mosesnagar
asked this question in
Ideas
Replies: 1 comment 2 replies
-
LINQ was first designed for side-effect free operations. That's why the don't add |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
posted it on csharplang, and it seems it related to this repo.
sometime you need repeat some code for X times. I mean code like that:
for(var i=0; i<X;i++) { .... var x = SomeGenerator(); SomeAction(x); ...... }
I found some ways to do it online, for example:
Enumerable.Repeat<Action>(() => { Console.WriteLine("Hello World"); this.DoSomething(); },10).ToList().ForEach(f => f.Invoke());
Enumerable.Repeat<Action>(() => { Console.WriteLine("Hello World."); this.DoSomeStuff(); }, 10).ToList().ForEach(x => x());
and more as you can see here for example:
https://stackoverflow.com/questions/6406741/is-there-an-elegant-way-to-repeat-an-action
but I don't find it very elegant. I thought about method extension for action, so we can do:
SomeAction().Repeat(times)
orRepeat(aciton, times) or Repeat(() => { ... }, times)
or3.Repeat(()=>{ ... });
WDYT?
Beta Was this translation helpful? Give feedback.
All reactions