87 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2023-10-18 10:16:10 +02:00
include::../description.adoc[]
2020-06-30 12:47:33 +02:00
2023-10-18 10:16:10 +02:00
include::../howtofix.adoc[]
2021-02-02 15:02:10 +01:00
2023-10-18 10:16:10 +02:00
Alternatively, adding the `static` keyword as class modifier will also prevent it from being instantiated.
2020-06-30 12:47:33 +02:00
=== Code examples
2020-06-30 12:47:33 +02:00
==== 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]
2020-06-30 12:47:33 +02:00
----
public class StringUtils // Noncompliant: explicit public constructor
2020-06-30 12:47:33 +02:00
{
public StringUtils()
{
}
2020-06-30 12:47:33 +02:00
public static string Concatenate(string s1, string s2)
{
return s1 + s2;
}
}
----
==== Compliant solution
2020-06-30 12:47:33 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
2023-10-18 10:16:10 +02:00
public static class StringUtils // Compliant: the class is static
2020-06-30 12:47:33 +02:00
{
public static string Concatenate(string s1, string s2)
{
return s1 + s2;
}
}
----
or
[source,csharp,diff-id=2,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
2023-10-18 10:16:10 +02:00
public class StringUtils // Compliant: the constructor is not public
2020-06-30 12:47:33 +02:00
{
2023-10-18 10:16:10 +02:00
private StringUtils()
2020-06-30 12:47:33 +02:00
{
}
2020-06-30 12:47:33 +02:00
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[]