Namespaces in C#


Introduction

This article bring in you to C# Namespaces. In this article, my objectives are as follows:

  • To give you the picture of 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 to organize your code. A good way to organize your namespaces is via a hierarchical system. You put the more general names at the top of the hierarchy and get more specific as you go down. This hierarchical system can be represented by nested namespaces.By placing code in different 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 name arun.CSharp.Namespaces is actually shorthand for defining  a namespace named arun that contains a namespace named CSharp that itself contains a 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 to qualify the use of a type in that namespace (a using directive).

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

Implement the "using" Directive

Next, we'll write a console application that uses the Hello class. We could just use the fully qualified name for the class-arun.CSharp.Namespaces.Hello-but this name is quite long and unwieldy. An easier way is to use a "using" directive, which makes it possible to use 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" directive you have seen in every program in this article. It allows you  to type the method names of members of the "System" namespace without typing the word "System" every time. In class Hello(), "Console" is a class member of the "System" namespace with the method "WriteLine". It's fully 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 of aliases. Such aliases can be useful in situation in which name collisions occur between two libraries, or when a small number of types from a much larger namespace are being used.

Using alias directives

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

using identifier = namespace-or-type-name ;

Within member declarations in a compilation unit or namespace body that contains a using-alias-directive, the identifier introduced by the using-alias-directive can be used to reference the given namespace or type.

For example:

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

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

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

The identifier of a using-alias-directive must be unique within the declaration space of the compilation unit or namespace that immediately contains the using-alias-directive.

Using namespace directives

A using-namespace-directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification.

using-namespace-directive:using namespace-name ;

Within member declarations in compilation unit or namespace body that contains a using-namespace-directive, the types contained in the given namespace 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 N3 namespace, the type members of N1.N2 are directly available, and thus class N3.B derives from class N1.N2.A. Like a using-alias-directive, a using-namespace-directive does not contribute any new members to the underlying declaration space of the compilation unit or namespace, but rather affects only the compilation unit or namespace body in which it appears.

The namespace-name referenced by a using-namespace-directive is resolved in the same way as the namespace-or-type-name referenced by a using-alias-directive. Thus, using-namespace-directives in the same compilation unit or namespace body 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. From this article we can understand the need and usage of Namespaces in classes.The Namespaces can hold other types also as follows:

Classes,Structures,Interfaces,Enumerations,Delegates.Namespaces are used both as an "internal" organization system for a program, and as an "external" organization system-a way of presenting program elements that are exposed to other programs.

Up Next
    Ebook Download
    View all
    Learn
    View all