rspec/rules/S2977/csharp/rule.adoc

58 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
Unfortunately, it is possible to make constructor calls recursive. When this happens, you get a class that cannot be instantiated.
As a general rule, no constructor should make a call to another constructor in the same class that requires fewer arguments than the calling constructor received. I.e. the constructor that accepts the most arguments is the one that has the fullest picture of how the class should look. It should perform class initialization.
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[source,csharp]
----
class Foo
{
int start;
Foo() : this(0) { }
Foo(int v) : this() { } // Noncompliant
}
----
=== Compliant solution
2022-02-04 17:28:24 +01:00
[source,csharp]
----
class Foo
{
int start;
Foo() : this(0) { }
Foo(int v)
{
this.count = v;
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
=== on 8 Jun 2015, 08:39:32 Tamas Vajk wrote:
LGTM, I've only added a "that" in the description. Can you validate that we actually need it. Thanks.
=== on 8 Jun 2015, 11:59:33 Ann Campbell wrote:
Looks fine [~tamas.vajk]. Thanks
=== on 15 Jun 2015, 06:59:50 Tamas Vajk wrote:
\[~ann.campbell.2] The default severity was missing, I've added it.
=== on 15 Jun 2015, 09:36:46 Tamas Vajk wrote:
This rule has been added to the C# compiler. Its error ID is CS0768.
endif::env-github,rspecator-view[]