2.10 Namespaces

These are defined in files, organized by namespaces, compiled into a module, then grouped into an assembly.

These organizational units are crosscutting. For example, typically a group of namespaces belong to one assembly, but a single namespace may in fact be spread over multiple assemblies (see Chapter 12).

2.10.1 Files

File organization is almost of no significance to the C# compiler ”a whole project could be merged into one .cs file and it would still compile (preprocessor statements are the only exception to this). However, it's generally tidy to have one type in one file, with the filename matching the name of the class and the directory the file is in matching the name of the class's namespace.

2.10.2 Namespaces

 namespace   name   +           // Dot-delimited {   using-statement*   [   namespace-declaration     type-declaration   ]*      // No delimiters } 

A namespace lets you group related types into a hierarchical categorization. Generally the first name is the name of your organization, and it gets more specific from there:

 namespace MyCompany.MyProduct.Drawing {   class Point {int x, y, z;}   delegate void PointInvoker(Point p); } 
2.10.2.1 Nesting namespaces

You may also nest namespaces instead of using dots. This example is semantically identical to the previous example:

 namespace MyCompany {   namespace MyProduct {     namespace Drawing {       class Point {int x, y, z;}       delegate void PointInvoker(Point p);     }   } } 
2.10.2.2 Using a type with its fully qualified name

To use the Point from another namespace, you may refer to it with its fully qualified name. The namespace a type is within actually becomes part of the type name:

 namespace TestProject {   class Test {     static void Main( ) {       MyCompany.MyProduct.Drawing.Point x;     }   } } 
2.10.2.3 The using keyword

The using keyword is a convenient way to avoid using the fully qualified name of types in other namespaces. This example is semantically identical to our previous example:

 namespace TestProject {   using MyCompany.MyProduct.Drawing;   class Test {     static void Main( ) {       Point x;     }   } } 
2.10.2.4 Aliasing types and namespaces

Type names must be unique within a namespace. To avoid naming conflicts without having to use fully qualified names , C# allows you to specify an alias for a type or namespace. Here is an example:

 using sys = System;        // Namespace alias using txt = System.String; // Type alias class Test {   static void Main( ) {     txt s = "Hello, World!";     sys.Console.WriteLine(s); // Hello, World!     sys.Console.WriteLine(s.GetType( )); // System.String   } } 
2.10.2.5 Global namespace

The global namespace is the outermost level that all namespaces and types are implicitly declared in. When a type is not explicitly declared within a namespace, it may be used without qualification from any other namespace, since it a member of the global namespace. However, apart from the smallest programs, it is always good practice to organize types within logical namespaces.

In this example, the class Test is declared in the global namespace, so it can be used without qualification from the Noo namespace.

 class Test {   public static void Foo ( ) {     System.Console.WriteLine ("hello!");   } } namespace Noo {   class Test2 {     static void Main( ) {       Test.Foo( );     }   } } 


C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net