Not
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
TypeName |
StaticHolderTypesShouldBeSealed |
CheckId |
CA1052 |
Category |
Microsoft.Design |
Breaking Change |
Breaking |
Cause
A public or protected type contains only static members and is not declared with the sealed (C# Reference) (NotInheritable) modifier.
Rule Description
This rule 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 sealed modifier to prohibit its use as a base type.
How to Fix Violations
To fix a violation of this rule, mark the type as sealed.
When to Exclude Warnings
Exclude a warning from this rule only if the type is designed to be inherited. The absence of the sealed modifier suggests that the type is useful as a base type.
Example
The following example shows a type that violates the rule.
Imports System
Namespace DesignLibrary
Public Class StaticMembers
Private Shared someField As Integer
Shared Property SomeProperty As Integer
Get
Return someField
End Get
Set
someField = Value
End Set
End Property
Private Sub New()
End Sub
Shared Sub SomeMethod()
End Sub
End Class
End Namespace
using System;
namespace DesignLibrary
{
public class StaticMembers
{
static int someField;
public static int SomeProperty
{
get
{
return someField;
}
set
{
someField = value;
}
}
StaticMembers() {}
public static void SomeMethod() {}
}
}
using namespace System;
namespace DesignLibrary
{
public ref class StaticMembers
{
static int someField;
StaticMembers() {}
public:
static property int SomeProperty
{
int get()
{
return someField;
}
void set(int value)
{
someField = value;
}
}
static void SomeMethod() {}
};
}