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 descriptionRule 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 violationsTo 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:
static
modifier suggests that the type is useful as a base type.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 analyzeUse 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 surfacesYou can configure which parts of your codebase to run this rule on, based on their accessibility, by setting the api_surface option. 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
Note
Replace the XXXX
part of CAXXXX
with the ID of the applicable rule.
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() { }
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4