Created
June 17, 2011 04:49
-
-
Save abombss/1030887 to your computer and use it in GitHub Desktop.
C# Safe navigation operator as extension
This file contains hidden or 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 class Extensions | |
{ | |
public static TResult SafeInvoke<TModel, TResult>(this TModel model, Func<TModel, TResult> expression, TResult nullValue = default(TResult)) | |
{ | |
try | |
{ | |
return expression(model); | |
} | |
catch (NullReferenceException) | |
{ | |
return nullValue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment