rspec/rules/S3442/csharp/rule.adoc
Costin Zaharia e6db263ce7
Modify rule 3442: apply LaYC format (#2194)
## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
2023-06-15 09:34:26 +02:00

47 lines
1.3 KiB
Plaintext

== Why is this an issue?
The https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract[abstract] modifier in a class declaration is used to indicate that a class is intended only to be a base class of other classes, not instantiated on its own.
Since `abstract` classes cannot be instantiated, there is no need for `public` or `internal` constructors. If there is basic initialization logic that should run when an extending class instance is created, you can add it in a `private`, `private protected` or `protected` constructor.
== How to fix it
Restrict the constructor visibility to the minimum: `private`, `private protected` or `protected`, depending on the usage.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
abstract class Base
{
public Base() // Noncompliant: should be private, private protected or protected.
{
//...
}
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
abstract class Base
{
protected Base()
{
//...
}
}
----
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract[abstract keyword]
* https://en.wikipedia.org/wiki/Abstract_type[abstract type]
include::../rspecator.adoc[]