rspec/rules/S2933/csharp/rule.adoc

66 lines
1.3 KiB
Plaintext
Raw Normal View History

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[]
2020-06-30 12:48:07 +02:00
=== Code examples
2020-06-30 12:48:07 +02:00
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2020-06-30 12:48:07 +02:00
----
public class Person
{
private int _birthYear; // Noncompliant
2020-06-30 12:48:07 +02:00
Person(int birthYear)
2020-06-30 12:48:07 +02:00
{
_birthYear = birthYear;
}
}
----
==== Compliant solution
2020-06-30 12:48:07 +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;
Person(int birthYear)
2020-06-30 12:48:07 +02:00
{
_birthYear = birthYear;
}
}
----
== Resources
2020-06-30 12:48:07 +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]
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[]