Why is this an issue?
Calling ToString()
on an object should always return a string. Returning null
instead contravenes the method’s implicit
contract.
Noncompliant code example
public override string ToString ()
{
if (this.collection.Count == 0)
{
return null; // Noncompliant
}
else
{
// ...
}
}
Compliant solution
public override string ToString ()
{
if (this.collection.Count == 0)
{
return string.Empty;
}
else
{
// ...
}
}
Resources