87 lines
1.5 KiB
Plaintext
87 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
include::../howtofix.adoc[]
|
|
|
|
Alternatively, adding the `static` keyword as class modifier will also prevent it from being instantiated.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
public class StringUtils // Noncompliant: implicit public constructor
|
|
{
|
|
public static string Concatenate(string s1, string s2)
|
|
{
|
|
return s1 + s2;
|
|
}
|
|
}
|
|
----
|
|
|
|
or
|
|
|
|
[source,csharp,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
public class StringUtils // Noncompliant: explicit public constructor
|
|
{
|
|
public StringUtils()
|
|
{
|
|
}
|
|
|
|
public static string Concatenate(string s1, string s2)
|
|
{
|
|
return s1 + s2;
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
public static class StringUtils // Compliant: the class is static
|
|
{
|
|
public static string Concatenate(string s1, string s2)
|
|
{
|
|
return s1 + s2;
|
|
}
|
|
}
|
|
----
|
|
|
|
or
|
|
|
|
[source,csharp,diff-id=2,diff-type=compliant]
|
|
----
|
|
public class StringUtils // Compliant: the constructor is not public
|
|
{
|
|
private StringUtils()
|
|
{
|
|
}
|
|
|
|
public static string Concatenate(string s1, string s2)
|
|
{
|
|
return s1 + s2;
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Hide this public constructor by making it ["protected"|"private"].
|
|
|
|
Add a ["protected"|"private"] constructor or the "static" keyword to the class declaration.
|
|
|
|
'''
|
|
|
|
endif::env-github,rspecator-view[]
|