2021-01-27 13:42:22 +01:00
|
|
|
Calling ``++ToString()++`` on an object should always return a string. Returning ``++null++`` instead contravenes the method's implicit contract.
|
2020-06-30 12:48:07 +02:00
|
|
|
|
|
|
|
== 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
|
|
|
|
{
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|
2021-06-02 20:44:38 +02:00
|
|
|
|
|
|
|
ifdef::rspecator-view[]
|
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::rspecator-view[]
|