Calling ToString() on an object should always return a
string
. Thus, overriding the
ToString method should never return null
, as it breaks the method’s implicit contract, and as a result the consumer’s
expectations.
public override string ToString ()
{
if (this.collection.Count == 0)
{
return null; // Noncompliant
}
else
{
// ...
}
}
A better alternative is to use the String.Empty built-in field.
public override string ToString ()
{
if (this.collection.Count == 0)
{
return string.Empty;
}
else
{
// ...
}
}