Događaji
Izgradite inteligentne aplikacije
M03 17 21 - M03 21 10
Pridružite se seriji susreta kako biste sa kolegama programerima i stručnjacima izgradili skalabilna AI rješenja zasnovana na stvarnim slučajevima korištenja.
Registrirajte seOvaj preglednik više nije podržan.
Nadogradite na Microsoft Edge da iskoristite najnovije osobine, sigurnosna ažuriranja i tehničku podršku.
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 9 | No |
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.
Bilješka
In the latest 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:
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.
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.
You 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
Bilješka
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
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() { }
}
.NET povratne informacije
.NET je projekat otvorenog koda. Odaberite vezu za pružanje povratnih informacija:
Događaji
Izgradite inteligentne aplikacije
M03 17 21 - M03 21 10
Pridružite se seriji susreta kako biste sa kolegama programerima i stručnjacima izgradili skalabilna AI rješenja zasnovana na stvarnim slučajevima korištenja.
Registrirajte se