Skip to main content

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.

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.

Block-Scoped Namespace Declaration

The traditional method of declaring a namespace uses the namespace keyword followed by a valid C# identifier and a set of curly braces { }. All types declared within the braces belong to that namespace.
namespace Company.Product.Module
{
    class Component
    {
        // Fully qualified name: Company.Product.Module.Component
    }
}

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 the namespace keyword, the identifier, and a terminating semicolon ;.
namespace Company.Product.Module;

class Component
{
    // Fully qualified name: Company.Product.Module.Component
}

interface IService
{
    // Fully qualified name: Company.Product.Module.IService
}
Rules for File-Scoped Namespaces:
  • 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):
namespace Root.Parent.Child
{
    class Node { }
}
Using Nested Blocks:
namespace Root
{
    namespace Parent
    {
        namespace Child
        {
            class Node { }
        }
    }
}

Technical Characteristics and Constraints

  • Implicit Access: Namespaces are implicitly public. You cannot apply access modifiers (such as public, private, or internal) 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