
org.sonar.plugins.csharp.S2225.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonarlint-omnisharp-plugin Show documentation
Show all versions of sonarlint-omnisharp-plugin Show documentation
Code Analyzer based on Omnisharp
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 - 2025 Weber Informatics LLC | Privacy Policy