Edit

Share via


CA1052: Static holder types should be Static or NotInheritable

Property Value
Rule ID CA1052
Title Static holder types should be Static or NotInheritable
Category Design
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 8 No

Cause

A non-abstract type contains only static members (other than a possible default constructor) and is not declared with the static or Shared modifier.

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

Rule description

Rule CA1052 assumes that a type that contains only static members is not designed to be inherited, because the type does not provide any functionality that can be overridden in a derived type. A type that is not meant to be inherited should be marked with the static modifier in C# to prohibit its use as a base type. Additionally, its default constructor should be removed. In Visual Basic, the class should be converted to a module.

This rule does not fire for abstract classes or classes that have a base class. However, the rule does fire for classes that support an empty interface.

Note

In the latest analyzer implementation of this rule, it also encompasses the functionality of rule CA1053.

How to fix violations

To fix a violation of this rule, mark the type as static and remove the default constructor (C#), or convert it to a module (Visual Basic).

When to suppress warnings

You can suppress violations in the following cases:

  • The type is designed to be inherited. The absence of the static modifier suggests that the type is useful as a base type.
  • The type is used as a type argument. Static types can't be used as type arguments.

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 CA1052
// The code that's violating the rule is on this line.
#pragma warning restore CA1052

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

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

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

Configure code to analyze

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

You can configure this option 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

Example of a violation

The following example shows a type that violates the rule:

public class StaticMembers
{
    public static int SomeProperty { get; set; }
    public static void SomeMethod() { }
}
Imports System

Namespace ca1052

    Public Class StaticMembers

        Shared Property SomeProperty As Integer

        Private Sub New()
        End Sub

        Shared Sub SomeMethod()
        End Sub

    End Class

End Namespace

Fix with the static modifier

The following example shows how to fix a violation of this rule by marking the type with the static modifier in C#:

public static class StaticMembers
{
    public static int SomeProperty { get; set; }
    public static void SomeMethod() { }
}