CSharp - Namespace Namespaces

Introduction

A namespace is used to group types.

Types are typically organized into hierarchical namespaces to avoid conflicts.

For example, the Console type is defined within the following namespace:

System

You can reference a type via its namespace. The following code calls Console's Write method:

System.Console.Write("book2s.com");

namespace keyword

The namespace keyword creates a namespace for types. For example:


namespace Outer.Middle.Inner
{
       class Class1 {}
       class Class2 {}
}

The dots in the namespace indicate a hierarchy of namespaces.

The code above is semantically identical to the following example:

namespace Outer
{
       namespace Middle
       {
         namespace Inner
         {
           class Class1 {}
           class Class2 {}
         }
       }
}

Qualified name

You can refer to a type with its fully qualified name by adding its namespace.

System.Console.Write("book2s.com");
Outer.Middle.Inner.Class1

Types defined outside namespace are in the global namespace.

The global namespace includes top-level namespaces.