Skip to content

Instantly share code, notes, and snippets.

@abombss
Created June 17, 2011 04:49
Show Gist options
  • Save abombss/1030887 to your computer and use it in GitHub Desktop.
Save abombss/1030887 to your computer and use it in GitHub Desktop.
C# Safe navigation operator as extension
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