Namespaces In C#


Introduction :

This article bring in you to C# Namespaces.
MY objectives are as follows:

To give the picture what Namespace is.
Learn how to implement the "using" directive.
Learn to use "alias" directives.
Understand what are namespace members..

NEED FOR NAMESPACES :

Namespaces allow you to create a system toorganize your code. A good way to organize your namespaces is via ahierarchical system. You put the more general names at the top of thehierarchy and get more specific as you go down. This hierarchicalsystem can be represented by nested namespaces.By placing code indifferent sub-namespaces, you can keep your code organized.

The example

namespace arun.CSharp.Namespaces
{
public class Hello
{
public string GetMessage() {
return "Hello, world";
}
}
}

Namespaces are hierarchical, and the namearun.CSharp.Namespaces is actually shorthand for defining a namespacenamed arun that contains a namespace named CSharp that itself containsa namespace named Namespaces, as in:

namespace arun
{
namespace CSharp
{
namespace Namespaces
{….}
}
}

The using keyword has two uses:

? Create an alias for a namespace (a using alias).
?Permit the use of types in a namespace, such that, you do not have toqualify the use of a type in that namespace (a using directive).

Java programmers should note that, we coulduse namespace first, followed by using or viceversa.The only purpose ofthe using command in this context is to save you typing and make yourcode simpler. It does not, for example, cause any other code orlibraries to be added to your project. If your code uses base classes,you need to ensure separately that the compiler knows which assembliesto look in for the classes (/r switch in the compiler).

IMPLEMENT THE "using" DIRECTIVE

Let us write a console application that usesthe Hello class. We could just use the fully qualified name for theclass ?arun.CSharp.Namespaces.Hello ? but this name is quite long andunwieldy.An easier way is to use a ?using? directive, which makes it possible touse all of the types in a namespace without qualification.

If you would like to call methods without typing their fully qualified name, you can implement the "using" directive.

using sysem;
using arun.CSharp.Namespaces;
class Hello
{
static void Main() {
Hello m = new Hello ();
System.Console.WriteLine(m.GetMessage());
}
}

"using System", is the same "using" directiveyou have seen in every program in this article. It allows you to typethe method names of members of the "System" namespace without typingthe word "System" every time. In class Hello(), "Console" is a classmember of the "System" namespace with the method "WriteLine". It'sfully qualified name is "System.Console.WriteLine(…)".

Note that the two occurrences of Hello are shorthand for arun.CSharp.Namespaces.Hello.

C# also enables the definition and use ofaliases. Such aliases can be useful in situation in which namecollisions occur between two libraries, or when a small number of typesfrom a much larger namespace are being used.

Using alias directives

A using-alias-directive introduces anidentifier that serves as an alias for a namespace or type within theimmediately enclosing compilation unit or namespace body.

using identifier = namespace-or-type-name ;Within member declarations in a compilation unit or namespace body thatcontains a using-alias-directive, the identifier introduced by theusing-alias-directive can be used to reference the given namespace ortype.

For example:

namespace N1.N2
{
class A {}
}
namespace N3
{
using A = N1.N2.A;
class B: A {}
}

Here, within member declarations in the N3namespace, A is an alias for N1.N2.A, and thus class N3.B derives fromclass N1.N2.A. The same effect can be obtained by creating an alias Rfor N1.N2 and then referencing R.A:

namespace N3
{
using R = N1.N2;
class B: R.A {}
}

The identifier of a using-alias-directive mustbe unique within the declaration space of the compilation unit ornamespace that immediately contains the using-alias-directive.

Using namespace directives

A using-namespace-directive imports the typescontained in a namespace into the immediately enclosing compilationunit or namespace body, enabling the identifier of each type to be usedwithout qualification.
using-namespace-directive:
using namespace-name ;
Within member declarations in compilation unit or namespace body thatcontains a using-namespace-directive, the types contained in the givennamespace can be referenced directly.

For example:

namespace N1.N2
{
class A {}
}
namespace N3
{
using N1.N2;
class B: A {}
}

Here, within member declarations in the N3namespace, the type members of N1.N2 are directly available, and thusclass N3.B derives from class N1.N2.A.

Like a using-alias-directive, ausing-namespace-directive does not contribute any new members to theunderlying declaration space of the compilation unit or namespace, butrather affects only the compilation unit or namespace body in which itappears.

The namespace-name referenced by ausing-namespace-directive is resolved in the same way as thenamespace-or-type-name referenced by a using-alias-directive. Thus,using-namespace-directives in the same compilation unit or namespacebody do not affect each other and can be written in any order.

Conclusion :

C# programs are organized using namespaces.Using directives are provided to facilitate the use of namespaces.Fromthis article we can understand the need and usage of Namespaces inclasses.The Namespaces can hold other types also as follows:

Classes,Structures,Interfaces,Enumerations,Delegates.Namespacesare used both as an ?internal? organization system for a program, andas an ?external? organization system ? a way of presenting programelements that are exposed to other programs.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!