Occasionally I have a need to retry an operation several times before giving up. My code is like:
int retries = 3; while(true) { try { DoSomething();
I'm a fan of recursion and extension methods, so here are my two cents:
public static void InvokeWithRetries(this Action @this, ushort numberOfRetries) { try { @this(); } catch { if (numberOfRetries == 0) throw; InvokeWithRetries(@this, --numberOfRetries); } }