Namespaces

Namespaces are a flexible alternative to Java packages, providing a mechanism to organize types into a logical hierarchical structure and ensuring globally unique type names.

Declaring Namespaces

Java classes and interfaces are allocated to a package by using the package statement at the start of a source file. All types declared in that file become members of the specified package. In C#, membership of a namespace is specified by declaring types within the body of a namespace statement. This enables C# to support multiple namespaces in a single file, eliminating the Java requirement for namespace hierarchies to map to file system directory structures. In the following example, both MyClass and MyInterface are members of the namespace Com.MyCompany:

namespace Com.MyCompany {
    // Namespace member declarations
    public class MyClass{
        // Implementation code for MyClass
    }

    public interface MyInterface{
        // Implementation code for MyInterface
    }

    // A nested namespace declaration
    namespace Tools {
        // Namespace member declaration
        public class MyClass{
            // Implementation code for MyClass
        }
    } // End of Tools namespace
} // End of Com.MyCompany namespace

Notice that we declare a nested namespace named Tools (which has the fully qualified name Com.Namespace.Tools) and two class definitions for MyClass. This is permitted because each MyClass declaration is contained within distinct namespaces, giving them the following fully qualified names:

Com.MyCompany.MyClass
Com.MyCompany.Tools.MyClass

When you declare a nested namespace, you cannot use a hierarchical name in a single declaration. You must declare multiple single-level nested declarations. For example, instead of this,

namespace TopLevel {
    namespace SecondLevel.ThirdLevel {
        ...
    }
}

the appropriate technique is this:

namespace TopLevel {
    namespace SecondLevel {
        namespace ThirdLevel {
            ...
        }
    }
}

The following program elements can be members of a namespace: class, struct, interface, enum, delegate, and namespace. If no namespace is specified, any declared members will be members of the default global namespace. Members of the global namespace are accessible without using any namespace; see the next section for more information.

Namespaces implicitly have public accessibility; they are not valid targets for access modifiers. The accessibility of namespace members is determined independently based on the access modifiers applied to them. See the Types section in Chapter 5 for details.

Using Namespaces

The C# using statement is the equivalent of the Java import statement, and it makes members of the specified namespace available for use without requiring a fully qualified name. When a namespace is imported, all of the members in that namespace are made available; unlike the Java import statement, using does not allow the programmer to specify an individual member to be imported.

If two imported namespaces contain members with the same name, the fully qualified names are used to differentiate between the members. C# also provides a mechanism for defining an alias that can be used to represent the namespace portion of the fully qualified member name. However, if an alias is declared for a namespace, members of the namespace cannot be accessed directly using their name and must always be prefixed with the alias.

The using statement operates at the namespace level; different namespaces in the same source file can import unique sets of namespaces. Each namespace is free to assign any aliases to the namespaces it imports, irrespective of those declared by other namespaces. An example of namespace usage follows:

namespace MyNamespace {
    using company = Com.MyCompany;
    using tools = Com.MyCompany.Tools;

    public class AClass : tools.MyClass, MyInterface {
        company.MyClass x = new company.MyClass();
    }
}

namespace MyOtherNamespace {
    using mycompany = Com.MyCompany;
    using utils = Com.MyCompany.Tools;

    public class BClass : utils.MyClass, MyInterface {
        mycompany.MyClass x = new mycompany.MyClass();
    }
}

In this example, the namespace Com.MyCompany is imported for use in the MyNamespace namespace and is assigned the alias company. The ambiguous class name MyClass is qualified using this alias.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset