liveBook · Manning

Namespace

As explained in .NET in Action, Second Edition

A namespace is a programming-language construct that groups related types and identifiers under a qualified name.

A namespace organizes .NET types under a qualified name. A using directive lets a file refer to types from a namespace without repeating the full name. section 2.4

In C#, namespaces help organize types and resolve their names:

  • Dots in a namespace name express a hierarchy: Regex belongs to System.Text.RegularExpressions. section 2.4
  • A namespace does not have to share its name with the NuGet package or assembly that provides its types. section 2.4
  • Top-level statements are outside a namespace; a separate class can be declared within one and called from the program. section 2.4
Listing 2.6 AsciiArt class using Figgle library to write text and type name
using Figgle;
  
namespace HelloDotnet;                              #1
  
public static class AsciiArt                        #2
{
  public static void Write(string text)
  {
    Console.WriteLine(FiggleFonts.Standard.Render(text));
    Console.WriteLine("Brought to you by "
      + typeof(AsciiArt).FullName);                 #3
  }
}

This example declares AsciiArt in HelloDotnet, uses Figgle, and prints the class’s full name. section 2.4

How .NET in Action, Second Edition covers it

How deep it goes
Explains dotted hierarchies and using directives through C# listings, including how a type’s FullName displays its namespace.
Examples
Uses Figgle and HelloDotnet.AsciiArt, then illustrates hierarchy with System.Text.RegularExpressions.Regex.
What you can do after
Apply C# namespace declarations and using directives to place a class and refer to types in another namespace.
How it connects
Connects namespaces to .NET types, using directives, top-level statements, assemblies, NuGet packages, and regular expressions.

Also explained in

Compare how 20 books explain namespace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.