Is there an elegant way to repeat an action?

后端 未结 9 1357
野的像风
野的像风 2020-12-16 08:56

In C#, using .NET Framework 4, is there an elegant way to repeat the same action a determined number of times? For example, instead of:

int repeat = 10;
for          


        
9条回答
  •  孤城傲影
    2020-12-16 09:42

    For brevity of a one liner you could do this. Not sure what you think...

    Enumerable.Repeat(() => 
    {
        Console.WriteLine("Hello World.");
        this.DoSomeStuff();
    }, 10).ToList().ForEach(x => x());
    

提交回复
热议问题