A static class in C# is a class declared with theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
static modifier that cannot be instantiated and is strictly limited to containing only static members and constant values. It serves as a structural container for state and behavior that is bound directly to the type itself, rather than to any specific object instance.
Core Architectural Rules
- Instantiation: The compiler strictly prohibits the use of the
newoperator on a static class. Attempting to instantiate it results in compile-time error CS0712. - Inheritance: Static classes are implicitly
sealed. They cannot be inherited by any other class. Furthermore, they cannot inherit from any class other thanSystem.Object, and they cannot implement interfaces. - Member Restrictions: Every field, property, method, and event within a static class must explicitly include the
staticmodifier. The inclusion of any instance member triggers a compile-time error. However, there are two critical exceptions regarding type members:- Constants:
constfields are implicitly static and are permitted. Explicitly applying thestaticmodifier to aconstfield results in compile-time error CS0106. - Nested Types: Nested types (classes, structs, enums, delegates, and interfaces) do not require the
staticmodifier. Furthermore, nested structs, enums, and interfaces are perfectly valid inside a static class but cannot be markedstaticat all.
- Constants:
- Extension Methods: Static classes are the only architectural construct in C# capable of hosting Extension Methods (methods defined with the
thismodifier on the first parameter). To host extension methods, the static class must be non-generic and non-nested.
Constructor Constraints
Static classes cannot contain instance constructors. They are permitted to have a single static constructor, subject to the following constraints:- It must be parameterless.
- It cannot have access modifiers (e.g.,
public,private). - It is invoked automatically by the Common Language Runtime (CLR) exactly once per Application Domain (AppDomain), strictly before any static member is referenced.
Memory Allocation and Lifecycle
When a C# application executes, the type object for a static class is loaded into the High-Frequency Heap of the AppDomain. Because there are no instances, memory is allocated only for the static fields defined within the class. The lifecycle of a static class and its state is intrinsically tied to the AppDomain. The CLR initializes the class upon first access, and the memory allocated for its static fields is not eligible for garbage collection until the AppDomain itself is unloaded. Consequently, state held within a static class is globally accessible and persists for the lifetime of the application.Master C# with Deep Grasping Methodology!Learn More





