dotnet / dotnet/docs

CA1052 suggests that the static keyword is meant to prevent inheritance

Open
#28,632 1 comment 0 reactions 0 assignees View on GitHub
dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

The current document currently gives the following explanation (emphasis mine):

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

Is that really the correct reasoning here? A type that is not meant to be inherited should be marked as **sealed** to prevent it from being used as a base type. But static-ness usually refers to whether or not it is useful to instantiate the type.

Maybe something like this instead, which immediately makes more sense to me?

> Rule CA1052 assumes that a type that contains only static members is not designed to be **instantiated**, because the type does not provide any functionality **as an instance**. A type that is not meant to be **instantiated** should be marked with the static modifier in C# to prohibit it **from being instantiated**.

I do see that the analyzer title “Static holder types should be Static or NotInheritable” does explicitly mention the “NotInheritable”, but the explanation still isn’t clear to me here.

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 23439dc2-302a-1b72-9aef-b1f0daab36c5
* Version Independent ID: 77b68df1-e7d1-36ea-4e4e-c167aa537a3a
* Content: [CA1052: Static holder types should be Static or NotInheritable (code analysis) - .NET](https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/CA1052)
* Content Source: [docs/fundamentals/code-analysis/quality-rules/ca1052.md](https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca1052.md)
* Product: **dotnet-fundamentals**
* GitHub Login: @gewarren
* Microsoft Alias: **gewarren**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.