How to write a function to repeat an action in C#

1 Answer

0 votes
using System;

class Program
{
    public static void Repeat(int count, Action action) {
        for (int i = 0; i < count; i++) {
            action();
        }
    }
    
    static void Main() {
        Repeat(5, () => Console.WriteLine("C# Programming"));
    }
}



 
/*
run:
 
C# Programming
C# Programming
C# Programming
C# Programming
C# Programming
 
*/

 



answered Jan 9, 2024 by avibootz
...