TypeName | SA1401FieldsMustBePrivate |
CheckId | SA1401 |
Category | Maintainability Rules |
A field within a C# class has an access modifier other than private.
A violation of this rule occurs whenever a field in a class is given non-private access. For maintainability reasons, properties should always be used as the mechanism for exposing fields outside of a class, and fields should always be declared with private access. This allows the internal implementation of the property to change over time without changing the interface of the class.
Fields located within C# structs are allowed to have any access level.
Fields that are static and readonly will not raise a violation. These kinds of fields are commonly used to represent a constant value when the const
keyword cannot be used, and therefore they are exempt from this rule.
To fix a violation of this rule, make the field private and add a property to expose the field outside of the class.
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Reviewed.")]
#pragma warning disable SA1401 // FieldsMustBePrivate
#pragma warning restore SA1401 // FieldsMustBePrivate