Namespaces In C#

Namespaces in C# allow you to organize a set of types in a group. In C#, every class belongs to a namespace and the namespace needs to be referenced or imported to use the class.

Introduction

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

  • To give you a picture of what Namespace is
  • To teach you how to implement the "using" directive
  • To teach you how to use "alias" directives
  • To help you understand what namespace members are
Namespaces in C#
 

Need for Namespaces

Namespaces allow you to create a system to organize your code in a group. 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,

  1. namespace arun.CSharp.Namespaces {  
  2.     public class Hello {  
  3.         public string GetMessage() {  
  4.             return "Hello, world";  
  5.         }  
  6.     }  
  7. }  

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,

  1. namespace arun {  
  2.     namespace CSharp {  
  3.         namespace Namespaces {  
  4.             ....  
  5.         }  
  6.     }  
  7. }  

The using keyword has two uses,

  • To create an alias for a namespace (a using alias).
  • To 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.

  1. using sysem;  
  2. using arun.CSharp.Namespaces;  
  3. class Hello {  
  4.     static void Main() {  
  5.         Hello m = new Hello();  
  6.         System.Console.WriteLine(m.GetMessage());  
  7.     }  
  8. }  

"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.

  1. 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,

  1. namespace N1.N2 {  
  2.     class A {}  
  3. }  
  4. namespace N3 {  
  5.     using A = N1.N2.A;  
  6.     class B: A {}  
  7. }  

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,

  1. namespace N3 {  
  2.     using R = N1.N2;  
  3.     class B: R.A {}  
  4. }  

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.

  1. using-namespace-directive:usingnamespace-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,

  1. namespace N1.N2 {  
  2.     class A {}  
  3. }  
  4. namespace N3 {  
  5.     using N1.N2;  
  6.     class B: A {}  
  7. }  

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, and 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.