Like this?
using System.Linq;
Enumerable.Range(0, 10).ForEach(arg => toRepeat());
This will execute your method 10 times.
[Edit]
I am so used to having ForEach
extension method on Enumerable, that I forgot it is not part of FCL.
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
action(item);
}
Here is what you can do without ForEach
extension method:
Enumerable.Range(0, 10).ToList().ForEach(arg => toRepeat());
[Edit]
I think that the most elegant solution is to implement reusable method:
public static void RepeatAction(int repeatCount, Action action)
{
for (int i = 0; i < repeatCount; i++)
action();
}
Usage:
RepeatAction(10, () => { Console.WriteLine("Hello World."); });
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…