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.
using static directive imports the static members and nested types of a specific type directly into the current compilation unit or namespace scope. This allows developers to invoke static methods, properties, fields, events, and constants without qualifying them with their declaring type name.
Syntax
using directive, which imports all types within a namespace, using static targets a specific class, struct, interface, or enum.
Mechanics and Scope
When the compiler encounters ausing static directive, it modifies the name resolution rules for the current file or namespace block.
Included Members:
- Static Methods, Properties, and Fields: Accessed directly by their identifier.
- Constants: Because
constfields are implicitly static, they are imported. - Nested Types: Types declared within the target type become accessible without qualification.
- Enum Members: When applied to an enumeration, the individual enum values are imported into the local scope.
- Instance Members: The directive strictly ignores instance-level methods, properties, and fields.
- Extension Methods (Unqualified): Extension methods declared in the target type are brought into scope for extension method invocation (e.g.,
object.ExtensionMethod()), but they are not imported for unqualified static invocation. To call them as standard static methods, the type name must still be provided.
Code Visualization
Name Resolution and Ambiguity Rules
The C# compiler enforces strict precedence rules to handle naming collisions whenusing static is employed:
- Local Precedence: Members declared within the current type always take precedence over members imported via
using static. If a local method and an imported static method share the same signature, the compiler binds to the local method. - Ambiguous Invocation: If multiple
using staticdirectives import members with identical names and signatures from different types, the compiler does not raise an error at the directive declaration. However, if that ambiguous member is invoked, the compiler throws error CS0121 (The call is ambiguous between the following methods or properties). The invocation must then be explicitly qualified with the type name.
Global Modifier (C# 10+)
Theusing static directive can be combined with the global modifier to apply the static import across all compilation units within the project.
Master C# with Deep Grasping Methodology!Learn More





