Namespaces in C#
↑Quick overview
Section titled “Quick overview”A namespace gives related types a shared name and prevents unrelated classes from colliding. It is not a security boundary, a directory, or a compiled assembly. A using directive makes a namespace easier to refer to; it does not import source files into your program.
The problem namespaces solve
Section titled “The problem namespaces solve”Imagine two packages defining a Logger class. Without an enclosing namespace, both classes would compete for the same type name. HCSTech.Logging.Logger and Vendor.Diagnostics.Logger can coexist because their fully qualified names differ. You choose which to refer to, either explicitly or through using.
namespace HCSTech.Logging;
public sealed class Logger{ public void Write(string message) => Console.WriteLine(message);}
// In a separate source file:using HCSTech.Logging;Logger logger = new();logger.Write("Hello");// C++ namespaces group names, but are not C# assemblies.#include <iostream>namespace HCSTech::Logging { struct Logger { void write(const char* text) { std::cout << text << '\n'; } };}HCSTech::Logging::Logger logger;logger.write("Hello");Choose a naming scheme
Section titled “Choose a naming scheme”- Start with a stable root. For example,
HCSTech.GameplayorHCSTech.Tools. This reduces clashes with third-party packages. - Group by responsibility. Prefer
Gameplay.InventoryorGameplay.Combatto namespaces named after arbitrary folder numbers. - Keep public APIs understandable. Callers should be able to guess where a type belongs.
- Rename carefully. Moving a file does not require changing the namespace, but changing a namespace can break references and serialized type names.
File-scoped and block-scoped forms
Section titled “File-scoped and block-scoped forms”namespace MyApp.Core; applies to the whole file and avoids another indentation level. The older block form namespace MyApp.Core { ... } is useful when a file intentionally contains more than one namespace, but that is uncommon. Choose one convention and enforce it consistently.
Aliases and ambiguous names
Section titled “Aliases and ambiguous names”You can write using FileLogger = HCSTech.Logging.Logger; to give a type a local alias. If two imported namespaces contain the same class name, use a fully qualified name or an alias rather than relying on an ambiguous short name.
When this matters in Unity
Section titled “When this matters in Unity”Namespaces help separate runtime gameplay code from editor tooling and third-party systems. Unity may serialize references to script types, so reorganizing namespaces in an existing project deserves testing. Namespaces by themselves do not improve compilation speed; assembly definitions create the relevant compilation boundaries.
