Namespaces and Using in C#

Started by OmMuruga, Feb 01, 2009, 04:35 PM

Previous topic - Next topic

OmMuruga

Namespaces in the .NET Runtime are used to organize classes and other types into a single
hierarchical structure. The proper use of namespaces will make classes easy to use and prevent
collisions with classes written by other authors.
Namespaces can also be thought of as way to specify really long names for classes and other types
without having to always type a full name.
Namespaces are defined using the namespace statement. For multiple levels of organization,
namespaces can be nested:


namespace Outer
{
namespace Inner
{
class MyClass
{
public static void Function() {}
}
}
}


That's a fair amount of typing and indenting, so it can be simplified by using the following instead:

namespace Outer.Inner
{
class MyClass
{
public static void Function() {}
}
}



Each source file can define as many different namespaces as needed.
As mentioned in the "Hello, Universe" section, using allows the user to omit namespaces when using
a type, so that the types can be more easily referenced.
Using is merely a shortcut that reduces the amount of typing that is required when referring to
elements, as the following :

        USING CLAUSE                               SOURCE LINE
    <none>                                System.Console.WriteLine("Hello");
     using                                   System Console.WriteLine("Hello");


Note that using cannot be used with a class name, so that the class name could be omitted. In other
words, using System.Console is not allowed.
Collisions between types or namespaces that have the same name can always be resolved by a type's fully qualified name. This could be a very long name if the class is deeply nested, so there is a variant of the using clause that allows an alias to be defined to a class:


                   using ThatConsoleClass = System.Console;

class Hello
{
public static void Main()
{
ThatConsoleClass.WriteLine("Hello");
}
}

To make the code more readable, the examples in this book rarely use namespaces, but they should be used in most real code



Namespaces and Assemblies

               
               An object can be used from within a C# source file only if that object can be located by the C# compiler. By default, the compiler will only open the single assembly known as mscorlib.dll, which contains the core functions for the Common Language Runtime.
            To reference objects located in other assemblies, the name of the assembly file must be passed to the compiler. This can be done on the command line using the /r:<assembly> option, or from within the Visual Studio IDE by adding a reference to the C# project.
Typically, there is a correlation between the namespace that an object is in and the name of the
assembly in which it resides. For example, the types in the System.Net namespace reside in the
System.Net.dll assembly. Types are usually placed in assemblies based on the usage patterns of
the objects in that assembly; a large or rarely used type in a namespace might be placed in its own
assembly. The exact name of the assembly that an object is contained in can be found in the documentation for that object.

:acumen :acumen