| 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 |
|
|
51a3165d-781e-4a55-aa0d-ea25fee7d4f2 |
mikejo5000 |
mikejo |
jillfra |
|
|
| Item | Value |
|---|---|
| CheckId | CA1052 |
| Category | Microsoft.Design |
| Breaking change | Breaking |
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 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.
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).
You can suppress violations in the following cases:
- The type is designed to be inherited. The absence of the
staticmodifier 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.
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, internalYou 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.
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]
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() { }
}