Compartilhar via


Using Namespaces (C# Programming Guide)

Namespaces are heavily used within C# programs in two ways. Firstly, the .NET Framework classes use namespaces to organize its many classes. Secondly, declaring your own namespaces can help control the scope of class and method names in larger programming projects.

Accessing Namespaces

Most C# applications begin with a section of using directives. This section lists the namespaces that the application will be using frequently, and saves the programmer from specifying a fully qualified name every time that a method that is contained within is used.

For example, by including the line:

using System;

At the start of a program, the programmer can use the code:

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

Instead of:

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

Namespace Aliases

The using Directive (C# Reference) can also be used to create an alias for a namespace. For example, if you are using a previously written namespace that contains nested namespaces, you might want to declare an alias to provide a shorthand way of referencing one in particular, as in the following example:

using Co = Company.Proj.Nested;  // define an alias to represent a namespace

Using Namespaces to control scope

The namespace keyword is used to declare a scope. The ability to create scopes within your project helps organize code and lets you create globally-unique types. In the following example, a class titled SampleClass is defined in two namespaces, one nested inside the other. The . Operator (C# Reference) is used to differentiate which method gets called.

namespace SampleNamespace
{
    class SampleClass
    {
        public void SampleMethod()
        {
            System.Console.WriteLine(
              "SampleMethod inside SampleNamespace");
        }
    }

    // Create a nested namespace, and define another class. 
    namespace NestedNamespace
    {
        class SampleClass
        {
            public void SampleMethod()
            {
                System.Console.WriteLine(
                  "SampleMethod inside NestedNamespace");
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Displays "SampleMethod inside SampleNamespace."
            SampleClass outer = new SampleClass();
            outer.SampleMethod();

            // Displays "SampleMethod inside SampleNamespace."
            SampleNamespace.SampleClass outer2 = new SampleNamespace.SampleClass();
            outer2.SampleMethod();

            // Displays "SampleMethod inside NestedNamespace."
            NestedNamespace.SampleClass inner = new NestedNamespace.SampleClass();
            inner.SampleMethod();
        }
    }
}

Fully Qualified Names

Namespaces and types have unique titles described by fully qualified names that indicate a logical hierarchy. For example, the statement A.B implies that A is the name of the namespace or type, and B is nested inside it.

In the following example, there are nested classes and namespaces. The fully qualified name is indicated as a comment following each entity.

namespace N1     // N1
{
    class C1      // N1.C1
    {
        class C2   // N1.C1.C2
        {
        }
    }
    namespace N2  // N1.N2
    {
        class C2   // N1.N2.C2
        {
        }
    }
}

In the previous code segment:

  • The namespace N1 is a member of the global namespace. Its fully qualified name is N1.

  • The namespace N2 is a member of N1. Its fully qualified name is N1.N2.

  • The class C1 is a member of N1. Its fully qualified name is N1.C1.

  • The class name C2 is used two times in this code. However, the fully qualified names are unique. The first instance of C2 is declared inside C1; therefore, its fully qualified name is: N1.C1.C2. The second instance of C2 is declared inside a namespace N2; therefore, its fully qualified name is N1.N2.C2.

Using the previous code segment, you can add a new class member, C3, to the namespace N1.N2 as follows:

namespace N1.N2
{
    class C3   // N1.N2.C3
    {
    }
}

In general, use :: to reference a namespace alias or global:: to reference the global namespace and . to qualify types or members.

It is an error to use :: with an alias that references a type instead of a namespace. For example:

using Alias = System.Console;
class TestClass
{
    static void Main()
    {
        // Error 
        //Alias::WriteLine("Hi");

        // OK
        Alias.WriteLine("Hi");
    }
}

Remember that the word global is not a predefined alias; therefore, global.X does not have any special meaning. It acquires a special meaning only when it is used with ::.

Compiler warning CS0440 is generated if you define an alias named global because global:: always references the global namespace and not an alias. For example, the following line generates the warning:

using global = System.Collections;   // Warning

Using :: with aliases is a good idea and protects against the unexpected introduction of additional types. For example, consider this example:

using Alias = System;
namespace Library
{
    public class C : Alias.Exception { }
}

This works, but if a type named Alias were to subsequently be introduced, Alias. would bind to that type instead. Using Alias::Exception insures that Alias is treated as a namespace alias and not mistaken for a type.

See the topic How to: Use the Global Namespace Alias (C# Programming Guide) for more information regarding the global alias.

See Also

Reference

Namespaces (C# Programming Guide)

Namespace Keywords (C# Reference)

. Operator (C# Reference)

:: Operator (C# Reference)

extern (C# Reference)

Concepts

C# Programming Guide