Call Action / Func

public static void Call(this Action action)
        {
            if (action != null)
                action();
        }

        public static void Call<T>(this Action<T> action, T t)
        {
            if (action != null)
                action(t);
        }

        public static void Call<T1, T2>(this Action<T1, T2> action, T1 t1, T2 t2)
        {
            if (action != null)
                action(t1, t2);
        }

        public static void Call<T1, T2, T3>(this Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
        {
            if (action != null)
                action(t1, t2, t3);
        }

        public static R Call<R>(this Func<R> func, R r = default (R))
        { return (func != null) ? func() : r; }

        public static R Call<T, R>(this Func<T, R> func, T t, R r = default (R))
        { return (func != null) ? func(t) : r; }

        public static R Call<T1, T2, R>(this Func<T1, T2, R> func, T1 t1, T2 t2, R r = default (R))
        { return (func != null) ? func(t1, t2) : r; }

        public static R Call<T1, T2, T3, R>(this Func<T1, T2, T3, R> func, T1 t1, T2 t2, T3 t3, R r = default (R))
        { return (func != null) ? func(t1, t2, t3) : r; }
Example:
Action action = null;
action.Call();

Action<string> action1 = null;
action1.Call("This won't run");

action1 = str => Console.WriteLine(str);
action1.Call("This will run");

// Same behavior with the rest of the Func

Description

Allows user to call an action / func delegate without having to check for null delegate

Details

Double click on the code to select all.

 

;