Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Created March 24, 2014 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiyaowl/9742472 to your computer and use it in GitHub Desktop.
Save kamiyaowl/9742472 to your computer and use it in GitHub Desktop.
c# nullckeck
namespace kamiya.util{
public static class Extensions {
/// <summary>
/// targetがnullだった場合はnullを、そうでなければ実行します
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="target"></param>
/// <param name="f">存在した時に実行する関数</param>
/// <returns></returns>
public static T2 NullCheck<T1, T2>(this T1 target, Func<T1, T2> f) {
if (target == null) return default(T2);
else return f(target);
}
public static void NullCheck<T1>(this T1 target, Action<T1> f) {
if (target == null) return;
else f(target);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment