Created
November 23, 2013 22:55
-
-
Save gxclarke/7621080 to your computer and use it in GitHub Desktop.
Nullable Monad Pattern
Example: SiteMapBase.GetActualCurrentNode().IfNotNull(x => x.Title)
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
public static TResult IfNotNull<T, TResult>(this T input, Func<T, TResult> action, TResult valueIfNull) | |
where T : class | |
{ | |
if (input != null) return action(input); | |
else return valueIfNull; | |
} | |
public static TResult IfNotNull<T, TResult>(this T input, Func<T, TResult> action) | |
where T : class | |
where TResult : class | |
{ | |
return input.IfNotNull(action, null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment