What is the recommended practice on returning NULLs when implementing ToString? Is it strongly discouraged, or prohibited, or allowed?
ToString Returning a Null Reference
While the practice of overriding the ToString method to return a null reference is not prohibited, I would strongly discourage it.
The ToString method is designed to return a meaningful string representation of an object, and one that serves to identify the object. Where possible, it should return a string representation of the object's value (as is the case with strings, numbers, and dates). If that's not possible or desireable, it should return a string indicating the object's type (as is the case with collection objects, for example, or with the default implementation of the Object.ToString method).
For formatting operations, there's no particular harm in returning a null reference, since an attempt to use the returned null string in a formatting operation simply produces an empty string. For example, If null_String is a null string, the statement Console.WriteLine(nullString) results in an empty string being displayed to the console. Similarly, the statement String.Format("{0}", null_String) returns an empty string.
The major problem arises when attempting to call instance methods on the null string, since these method calls will always throw a NullReferenceException. And since a string returned by a call to an object's ToString method is assumed to be valid (that is, non-null), it's unlikely that developers who consume the type will check to see whether the string returned by ToString is non-null before calling its instance methods.
I hope that this helps to answer your question.
--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation