36 lines
534 B
Plaintext
36 lines
534 B
Plaintext
![]() |
Calling <code>ToString()</code> on an object should always return a string. Returning <code>null</code> 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
|
||
|
{
|
||
|
// ...
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
include::../see.adoc[]
|