Skip to content
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@sharwell @vweijsters
44 lines (31 sloc) 1.39 KB


TypeName SA1401FieldsMustBePrivate
CheckId SA1401
Category Maintainability Rules


A field within a C# class has an access modifier other than private.

Rule description

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.

How to fix violations

To fix a violation of this rule, make the field private and add a property to expose the field outside of the class.

How to suppress violations

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Reviewed.")]
#pragma warning disable SA1401 // FieldsMustBePrivate
#pragma warning restore SA1401 // FieldsMustBePrivate
You can’t perform that action at this time.