Skip to content

Latest commit

 

History

History
84 lines (61 loc) · 3.57 KB

File metadata and controls

84 lines (61 loc) · 3.57 KB
title ms.date ms.topic f1_keywords helpviewer_keywords ms.assetid author ms.author manager dev_langs ms.workload
CA1052: Static holder types should be Static or NotInheritable
07/25/2019
reference
StaticHolderTypesShouldBeSealed
CA1052
CA1052
StaticHolderTypesShouldBeSealed
51a3165d-781e-4a55-aa0d-ea25fee7d4f2
mikejo5000
mikejo
jillfra
CPP
CSharp
VB
multiple

CA1052: Static holder types should be Static or NotInheritable

Item Value
CheckId CA1052
Category Microsoft.Design
Breaking change Breaking

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 FxCop 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.

Configurability

If you're running this rule from FxCop analyzers (and not with legacy analysis), 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.ca1052.api_surface = private, internal

You can configure this option for just this rule, for all rules, or for all rules in this category (Design). For more information, see Configure FxCop analyzers.

Example of a violation

The following example shows a type that violates the rule:

[!code-csharpFxCop.Design.StaticMembers#1] [!code-vbFxCop.Design.StaticMembers#1] [!code-cppFxCop.Design.StaticMembers#1]

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() { }
}