53 lines
814 B
Plaintext
53 lines
814 B
Plaintext
Calling ``++ToString()++`` on an object should always return a string. Returning ``++null++`` instead contravenes the method's implicit contract.
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,csharp]
|
|
----
|
|
public override string ToString ()
|
|
{
|
|
if (this.collection.Count == 0)
|
|
{
|
|
return null; // Noncompliant
|
|
}
|
|
else
|
|
{
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
[source,csharp]
|
|
----
|
|
public override string ToString ()
|
|
{
|
|
if (this.collection.Count == 0)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
else
|
|
{
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
include::../see.adoc[]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|