CA1051: Do not declare visible instance fields

Property Value
Rule ID CA1051
Title Do not declare visible instance fields
Category Design
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 8 No

Cause

A type has a non-private instance field.

By default, this rule only looks at externally visible types, but this is configurable.

Rule description

The primary use of a field should be as an implementation detail. Fields should be private or internal and should be exposed by using properties. It's as easy to access a property as it is to access a field, and the code in the accessors of a property can change as the features of the type expand without introducing breaking changes.

Properties that just return the value of a private or internal field are optimized to perform on par with accessing a field; the performance gain from using externally visible fields instead of properties is minimal. Externally visible refers to public, protected, and protected internal (Public, Protected, and Protected Friend in Visual Basic) accessibility levels.

Additionally, public fields cannot be protected by Link demands. (Link demands don't apply to .NET Core apps.)

How to fix violations

To fix a violation of this rule, make the field private or internal and expose it by using an externally visible property.

When to suppress warnings

Only suppress this warning if you're certain that consumers need direct access to the field. For most applications, exposed fields do not provide performance or maintainability benefits over properties.

Consumers may need field access in the following situations:

  • In ASP.NET Web Forms content controls.
  • When the target platform makes use of ref to modify fields, such as model-view-viewmodel (MVVM) frameworks for WPF and UWP.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1051
// The code that's violating the rule is on this line.
#pragma warning restore CA1051

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1051.severity = none

For more information, see How to suppress code analysis warnings.

Include or exclude APIs

Use the following options to configure which parts of your codebase to run this rule on.

You can configure these options for just this rule, for all rules it applies to, or for all rules in this category (Design) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.api_surface = private, internal

Exclude structs

You can exclude struct (Structure in Visual Basic) fields from being analyzed.

dotnet_code_quality.ca1051.exclude_structs = true

Example

The following example shows a type (BadPublicInstanceFields) that violates this rule. GoodPublicInstanceFields shows the corrected code.

public class BadPublicInstanceFields
{
    // Violates rule DoNotDeclareVisibleInstanceFields.
    public int instanceData = 32;
}

public class GoodPublicInstanceFields
{
    private int instanceData = 32;

    public int InstanceData
    {
        get { return instanceData; }
        set { instanceData = value; }
    }
}

See also