Created
March 24, 2014 15:29
-
-
Save kamiyaowl/9742472 to your computer and use it in GitHub Desktop.
c# nullckeck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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