What is Namespace?

Namespace is the great features of .NET Framework which is basically used to organize our codes. This article describes basic overview of namespace or how to use namespace in C#. Summary of the article:

  • What is Namespace?
  • What is the Purpose of Namespaces?
  • Properties of Namespace
  • Defining a Namespace
  • Nested Namespaces

c#, c-sharp, #

What is Namespace?
Namespace is a method where all the classes are organized according to their functionality. It works like a container where all the classes are organized according to their functionality. We use it by using “using” key word. In C# we use system.web namespace for web related task. We used system namespace for all c# programs. This is the father namespace for all .Net Framework

Generally a namespace is designed for providing a way to keep one set of names separate from another. That means the class names declared in one namespace will not conflict with the same class names declared in another. It does not impact on the performance.

What is the Purpose of Namespaces?
The purpose of Namespaces in C# and other programming languages is:

  • To structure the project into meaningful piece
  • To distinguish classes with the same name

Properties of Namespace
According to MSDN a namespace has the following properties:

  • They organize large code projects
  • They are delimited with the . operator
  • We do not need to specify the name of the namespace for every class
  • The global namespace is the »root« namespace: global::System will always refer to the .NET Framework namespace System

Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace name as follows:

namespace MyNamespaceName
{
    // code declarations
}

Nested Namespaces
Namespaces can be nested where you can define one namespace inside another namespace as follows:

namespace MyNamespaceName1
{
    // code declarations
    namespace MyNamespaceName2
    {
        // code declarations
    }
}

We can access members of nested namespace by using the dot (.) operator.

Comments

  1. By Jenial D Kosta

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *