2023-10-13 08:05:03 +02:00
|
|
|
include::../why.adoc[]
|
2023-05-03 11:06:20 +02:00
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
=== 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.
|
2023-05-25 14:18:12 +02:00
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
include::../how.adoc[]
|
2020-06-30 12:48:07 +02:00
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
=== Code examples
|
2020-06-30 12:48:07 +02:00
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
2020-06-30 12:48:07 +02:00
|
|
|
----
|
|
|
|
public class Person
|
|
|
|
{
|
2023-10-13 08:05:03 +02:00
|
|
|
private int _birthYear; // Noncompliant
|
2020-06-30 12:48:07 +02:00
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
Person(int birthYear)
|
2020-06-30 12:48:07 +02:00
|
|
|
{
|
|
|
|
_birthYear = birthYear;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
==== Compliant solution
|
2020-06-30 12:48:07 +02:00
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
2020-06-30 12:48:07 +02:00
|
|
|
----
|
|
|
|
public class Person
|
|
|
|
{
|
|
|
|
private readonly int _birthYear;
|
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
Person(int birthYear)
|
2020-06-30 12:48:07 +02:00
|
|
|
{
|
|
|
|
_birthYear = birthYear;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Resources
|
2020-06-30 12:48:07 +02:00
|
|
|
|
2023-10-13 08:05:03 +02:00
|
|
|
=== 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]
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../message.adoc[]
|
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|