
## 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) --------- Co-authored-by: Zsolt Kolbay <121798625+zsolt-kolbay-sonarsource@users.noreply.github.com>
66 lines
1.3 KiB
Plaintext
66 lines
1.3 KiB
Plaintext
include::../why.adoc[]
|
|
|
|
=== Exceptions
|
|
|
|
* Fields declared in classes marked with the `Serializable` attribute.
|
|
* Fields declared in `partial` classes.
|
|
* Fields with attributes are ignored.
|
|
* Fields of type `struct` that are not primitive or pointer types are also ignored because of possible unwanted behavior.
|
|
|
|
include::../how.adoc[]
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
public class Person
|
|
{
|
|
private int _birthYear; // Noncompliant
|
|
|
|
Person(int birthYear)
|
|
{
|
|
_birthYear = birthYear;
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
public class Person
|
|
{
|
|
private readonly int _birthYear;
|
|
|
|
Person(int birthYear)
|
|
{
|
|
_birthYear = birthYear;
|
|
}
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly[readonly]
|
|
* Fabulous adventures in coding - https://ericlippert.com/2008/05/14/mutating-readonly-structs/[Mutating readonly structs]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|