rspec/rules/S3442/csharp/rule.adoc

47 lines
1.3 KiB
Plaintext
Raw Normal View History

== 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.
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
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]
2021-04-28 16:49:39 +02:00
----
abstract class Base
{
public Base() // Noncompliant: should be private, private protected or protected.
2021-04-28 16:49:39 +02:00
{
//...
}
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
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[]