2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-10-12 18:11:39 +02:00
include::../description.adoc[]
2020-06-30 14:49:38 +02:00
2023-10-12 18:11:39 +02:00
Note that due to optimizations on simple properties, public fields provide only very little performance gain.
include::../impacts.adoc[]
2020-06-30 12:47:33 +02:00
2023-10-12 18:11:39 +02:00
=== Exceptions
2020-06-30 12:47:33 +02:00
2023-10-12 18:11:39 +02:00
Fields marked as `readonly` or `const` are ignored by this rule.
2021-02-02 15:02:10 +01:00
2024-04-15 09:32:55 +02:00
Fields inside classes or structs annotated with `[StructLayout]` are ignored by this rule.
Fields inside classes or structs annotated with `[Serializable]` are ignored by this rule unless they are annotated with `[NonSerialized]`.
2023-10-12 18:11:39 +02:00
== How to fix it
Depending on your needs:
* Use auto-implemented properties: +
For common cases, where no validation is required, auto-implemented properties are a good alternative to fields: these allows fine grained access control and offers the flexibility to add validation or change internal storage afterwards.
__Note:__ as a bonus it is now possible to monitor value changes using breakpoints.
2020-06-30 12:47:33 +02:00
2023-10-12 18:11:39 +02:00
* Encapsulate the fields in your class. To do so:
2020-06-30 12:47:33 +02:00
2023-10-12 18:11:39 +02:00
. Make the field private.
. Use public properties (set and get) to access and modify the field.
* Mark field as `readonly` or `const`.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
public class Foo
{
2023-10-12 18:11:39 +02:00
public int InstanceData = 32; // Noncompliant
public int AnotherInstanceData = 32; // Noncompliant
2020-06-30 12:47:33 +02:00
}
----
2023-10-12 18:11:39 +02:00
==== Compliant solution
2020-06-30 12:47:33 +02:00
2023-10-12 18:11:39 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
public class Foo
{
2023-10-12 18:11:39 +02:00
// using auto-implemented properties
public int InstanceData { get; set; } = 32;
2020-06-30 12:47:33 +02:00
2023-10-12 18:11:39 +02:00
// using field encapsulation
private int _anotherInstanceData = 32;
public int AnotherInstanceData
2020-06-30 12:47:33 +02:00
{
2023-10-12 18:11:39 +02:00
get { return _anotherInstanceData; }
set
{
// perform validation
_anotherInstanceData = value;
}
2020-06-30 12:47:33 +02:00
}
2023-10-12 18:11:39 +02:00
2020-06-30 12:47:33 +02:00
}
----
2023-10-12 18:11:39 +02:00
=== Pitfalls
2020-06-30 12:47:33 +02:00
2023-10-12 18:11:39 +02:00
Please be aware that changing a field by a property in a software that uses serialization could lead to binary incompatibility.
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
include::../see.adoc[]
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)
2023-05-25 14:18:12 +02:00
=== Message
Make this field 'private' and encapsulate it in a 'public' property.
=== Highlighting
Field identifier.
2021-09-20 15:38:42 +02:00
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[]