Computer Passion

By Anthony J. Fusco

Check for null before calling ToString()

Check for null before calling ToString()

Handy little snippet of code for checking for null before calling ToString(). I found this little bit of code pretty clever and handy. Let us avoid those errors: private static string ToSafeString(object obj) {    return (obj ?? string.Empty).ToString(); } Source Share me… Facebook AccessDeniedAccess DeniedK21C5V02HNNR48RBEsXdYCrZ6vMDKmeu4+7E9AMu7q0E5Vh7HfAS702/jbASF8+bkoDAQQCk6Hzc1WzNzeWhIYmMMYM= AccessDeniedAccess DeniedJRXPF89EFX6YJD4AY8Ufw01gS6KOijpDIyfr4DExHh4KKkluIM/t/egesZkC7WliiRNkfTlDYeJXiYbFJcr8+FHSt5c=Google Twitter Pinterest Email Linkedin