Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created February 3, 2014 16:16
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 JohnLBevan/d96780f5aa12bd2f86d3 to your computer and use it in GitHub Desktop.
Save JohnLBevan/d96780f5aa12bd2f86d3 to your computer and use it in GitHub Desktop.
ToStringIfNotNull; allows me to write `return o.ToStringIfNotNull();` instead of `return o==null?null:o.ToString();` ; i.e. a ToString function which can cope with null references.
public static class ObjectExtensions
{
public static string ToStringIfNotNull(this object o)
{
return o == null ? null : o.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment