Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The namespace keyword

Reference for the C# namespace keyword: organize types, use file-scoped namespaces, using directives, global usings, aliases and static imports.

A namespace groups related types and prevents name clashes. Two classes called Parser can coexist if they live in different namespaces (Json.Parser and Xml.Parser). Namespaces are only a naming and organization tool: they do not control access (that is what public and internal are for) and do not have to match folder names, although they conventionally do.

Since C# 10 you can write a file-scoped namespace with a semicolon, which applies to the whole file and saves a level of indentation. A using directive imports a namespace so you can write short names.

C#
namespace Shop.Orders;

public class Order { }

Try it

C# Nested namespaces, using and aliases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# global:: and the global namespace

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
namespace Company.Product.Feature;        // file-scoped (one per file, before type declarations)

namespace Company.Product                 // block-scoped (several per file allowed)
{
    namespace Feature { ... }             // nested namespace
}

using System.Text;                        // import a namespace
using static System.Math;                 // import static members of a type
using Sb = System.Text.StringBuilder;     // alias (any type, since C# 12)
global using System.Linq;                 // for the entire project (C# 10)
global::Namespace.Type                    // start lookup at the global namespace

Errors: mixing a file-scoped namespace with block-scoped ones in one file is CS8955; a type name that exists in two imported namespaces is ambiguous (CS0104) and needs a qualifier or alias.

Usage notes

  • Default namespace: types declared with no namespace are in the global namespace. Avoid that outside tiny programs.
  • Implicit usings: SDK-style projects add common global usings; this runner provides System, System.Collections.Generic, System.IO, System.Linq, System.Net.Http, System.Threading and System.Threading.Tasks.
  • Order: using directives go before namespace declarations (or inside a block namespace). global using must come before ordinary usings in a file.
  • Top-level statements may only be followed by type and namespace declarations; they implicitly sit in the global namespace.
  • Nested name resolution: the compiler looks in the current namespace, then its parents, then usings. A local type can therefore hide an imported one.
  • Naming: use PascalCase and reverse-domain-style hierarchy (Company.Product.Module); do not name a namespace after a type in the same scope.
  • using also means resource disposal: using var x = ... is a different feature with the same keyword.