2020-06-30 12:48:07 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
|
|
|
----
|
|
|
|
public class Person
|
|
|
|
{
|
|
|
|
private int _birthYear; // Noncompliant
|
|
|
|
|
|
|
|
Person(int birthYear)
|
|
|
|
{
|
|
|
|
_birthYear = birthYear;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
----
|
|
|
|
public class Person
|
|
|
|
{
|
|
|
|
private readonly int _birthYear;
|
|
|
|
|
|
|
|
Person(int birthYear)
|
|
|
|
{
|
|
|
|
_birthYear = birthYear;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Exceptions
|
|
|
|
|
2020-06-30 14:49:38 +02:00
|
|
|
* Fields with attributes are ignored.
|
2020-12-23 14:59:06 +01:00
|
|
|
* Fields of type ``struct`` that are not primitive or pointer types are also ignored because of possible unwanted behavior.
|
2020-06-30 12:48:07 +02:00
|
|
|
|
|
|
|
== See
|
|
|
|
|
2020-06-30 14:49:38 +02:00
|
|
|
* https://ericlippert.com/2008/05/14/mutating-readonly-structs/[Mutating readonly structs]
|