Skip to content

Instantly share code, notes, and snippets.

@jonHuffman
Created March 1, 2017 20:55
Show Gist options
  • Save jonHuffman/e54210095c865141620d6ca29509bc23 to your computer and use it in GitHub Desktop.
Save jonHuffman/e54210095c865141620d6ca29509bc23 to your computer and use it in GitHub Desktop.
Adds a SafeInvoke method for C# Actions
using System;
public static class ActionExtensions
{
public static void SafeInvoke(this Action action)
{
if(action != null)
{
action();
}
}
public static void SafeInvoke<T>(this Action<T> action, T arg)
{
if (action != null)
{
action(arg);
}
}
public static void SafeInvoke<T1, T2>(this Action<T1, T2> action, T1 arg1, T2 arg2)
{
if (action != null)
{
action(arg1, arg2);
}
}
public static void SafeInvoke<T1, T2, T3>(this Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3)
{
if (action != null)
{
action(arg1, arg2, arg3);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment