A namespace declaration defines a named declarative region used to establish a hierarchical scope for types. It acts as a logical container that prevents naming collisions by appending a unique prefix to the fully qualified names of the types (classes, interfaces, structs, enums, and delegates) defined within it.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Block-Scoped Namespace Declaration
The traditional method of declaring a namespace uses thenamespace keyword followed by a valid C# identifier and a set of curly braces { }. All types declared within the braces belong to that namespace.
File-Scoped Namespace Declaration (C# 10+)
File-scoped namespace declarations eliminate indentation by applying the namespace to all types within the file. It is declared using thenamespace keyword, the identifier, and a terminating semicolon ;.
- A file can contain only one file-scoped namespace declaration.
- The declaration must precede any type declarations in the file.
- It cannot be combined with block-scoped namespace declarations in the same file.
Nested Namespace Declarations
Namespaces can be nested to create a deeper hierarchy. This can be achieved either through nested blocks or, more commonly, using dot notation. Using Dot Notation (Preferred):Technical Characteristics and Constraints
- Implicit Access: Namespaces are implicitly
public. You cannot apply access modifiers (such aspublic,private, orinternal) to a namespace declaration. - Compiler Merging: Namespace declarations are open-ended. If you declare the same namespace in multiple files, or multiple times within the same file, the C# compiler merges the members into a single namespace during compilation.
- Global Namespace: Any type declared outside of a specific namespace declaration belongs to the implicit “global” namespace. You can explicitly reference the global namespace using the
global::alias qualifier (e.g.,global::System.String). - Valid Identifiers: Namespace names must adhere to standard C# identifier rules (e.g., cannot start with a number, cannot be a reserved keyword unless prefixed with
@).
Master C# with Deep Grasping Methodology!Learn More





