Namespaces

Namespaces are a way of organizing the various types that occur in a C# program. It is somewhat similar in concept to a folder in a computer file system. Like folders, namespaces enable classes to have a unique fully-qualified name. A C# program contains one or more namespaces, and each namespace is either defined by you the programmer, or defined as part of a previously written class library.

For example, the namespace System includes the Console class, a class that contains methods for reading and writing to the console window. The System namespace also contains a number of other namespaces, such as System.IO and System.Collections. The .NET Framework alone has more than eighty namespaces, each with up to thousands of classes: namespaces are used to minimize the confusion that might be caused between similarly named types and methods.

If you write a class outside of a namespace declaration, the compiler will supply a default namespace for that class.

Namespace Shortcuts

To use the method WriteLine, defined in the Console class contained in the System namespace, you would use a line of code like this:

System.Console.WriteLine("Hello, World!");

Remembering to precede all the methods contained in Console with System would quickly become tiresome, so a useful shortcut is to insert the using Directive at the start of your C# source file, like this:

using System;

Including using System; establishes that the System namespace is assumed, and you can subsequently write just this:

Console.WriteLine("Hello, World!");

Creating Your Own Namespace

It is common to use namespaces when working on large programs. Using your own namespaces provides a degree of control over similarly named methods and types. For example, assume you are writing an application that loads both statistical data and image files from a disk. You could create two new namespaces, one called Images and one called StatisticalData. As you are using two separate namespaces, all the names of the methods defined in each namespace will be unique, even if the individual classes have the same name. This means you could have a class called FileHandling in both namespaces, both containing a method called Load. You could specify the class you wanted by referring to either StatisticalData.FileHandling or Images.FileHandling.

It is good practice to create a separate folder for each namespace in your Visual C# Express project.

Example

The following example defines two namespaces, each containing a class named FileHandling. By specifying the namespace, it's possible to quickly differentiate between the classes and the methods they contain.

namespace StatisticalData
{
    class FileHandling
    {
        public void Load() {}  // code to load statistical data
    }
}

namespace Images 
{
    class FileHandling
    {
        public void Load() {}  // code to load an image file
    }
}

class Program
{
    static void Main()
    {
        StatisticalData.FileHandling data = new StatisticalData.FileHandling();
        data.Load();

        Images.FileHandling image = new Images.FileHandling();
        image.Load();
    }
}

See Also

Concepts

C# Language Primer

Classes

Reference

Namespaces (C# Programming Guide)