rspec/rules/S2332/csharp/rule.adoc

40 lines
780 B
Plaintext
Raw Normal View History

== Why is this an issue?
Read-only fields and properties (properties with only an auto-implemented getter) can only be set in a constructor or as part of their declaration. A read-only member that isn't set in either place will retain its default value for the life of the object. At best, such properties clutter the source code. At worst, they are bugs.
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[source,csharp]
----
class Person
{
int Age { get; } // Noncompliant; will always be 0.
Person () {}
}
----
=== Compliant solution
2022-02-04 17:28:24 +01:00
[source,csharp]
----
class Person
{
int Age { get; } = 42;
Person () {}
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]