36 lines
720 B
Plaintext
36 lines
720 B
Plaintext
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
|
|
|
|
----
|
|
class Person
|
|
{
|
|
int Age { get; } // Noncompliant; will always be 0.
|
|
|
|
Person () {}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
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[]
|