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[]
