org.sonar.plugins.csharp.S2225.html Maven / Gradle / Ivy
Why is this an issue?
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
{
// ...
}
}
Resources
Documentation
© 2015 - 2024 Weber Informatics LLC | Privacy Policy