Topic : Namespace in C# Programming Language:

Namespace:

Code organization has always been on the top of priority lists of most of the programmer as organization results in more maintainable, manageable and reusable code. Object oriented programming paradigm was introduced in order to suffice the organization needs of code. However, embedding code inside a class was not enough because there can be thousands of classes. Managing a long list of classes without any intermediate organization mechanism is extremely laborious task which is also prone to errors. Therefore in order to a high level organizational entity was introduced in most of the advanced programming languages. In .NET frame work, namespace was introduced for this purpose.



  • Namespaces are heavily used in C# Programming Language in two different ways.
    » The First one is .NET Framework uses namespaces to Organize its many classes.
    » Second, Declaring our own namespaces can help us to control the scope of class and methods names in larger Programming projects.



1).NET Framework uses namespaces to Organize its many classes as follows:

System.Console.WriteLine("Enjoy Your Learning with InfoBrother:");

In Above example, we use system.console to print the message. where system is a namespace and console is a class in that namespace and WriteLine is the Method in console class.

using Keyword:

The Using keyword states that the Program is using the names in the given namespace. we can use this keyword so that the complete name is not required as above example. look at this example:

using System;
Console.WriteLine("Enjoy Your Learning with InfoBrother:");


2) We can declare our own Namespace to control the scope of class and method names in larger Programming projects. It is the way to keep one set of names separate from another. The Class Names declared in one namespace does not conflict with the same class names declared in another.


Defining a Namespace:

A Namespace definition begins with the keyword namespace followed by the namespace names as follows:


namespace   Namespace_name
{
    //Namespace body:
}

To call the Namespace, we can use dot operator with item name as follows:


Namespace_name.itemName;

Let's have an example to Know how we can define our namespace in C#:


/*Example - namespace - InfoBrother*/

using System;   

namespace NAME   //Name namespace:
{
    class Program
    {
       public void display()  //method to display name:
        {
            Console.WriteLine("Hi this is InfoBrother");
        }
    }
}


namespace AGE   //AGE namespace:
{
    class Program
    {
        public void display()  //Method to display age:
        {
            Console.WriteLine("I'M 22 Year old: ");
        }
    }
}


class Program 
{
    static void Main(string[] args)  //main method:
    {
        NAME.Program name = new NAME.Program(); //object of type name:
        AGE.Program age = new AGE.Program();   //object of type age.

        name.display();   //calling method to show name:
        age.display();    //calling method to show age:

        Console.ReadKey();
    }
}


Nested Namespaces:

we can define one namespace inside another namespace. and can access members of nested namespace by using the dot "." Operator as follows:



/*Example - Nested Namespace - InfoBrother*/

using System;   

namespace NAME   //Name namespace:
{
    class Program
    {
       public void display()  //method to display name:
        {
            Console.WriteLine("Hi this is InfoBrother");
        }
    }
     
    namespace AGE   //AGE namespace - Nested:
    {
        class Program
        {
            public void display()  //Method to display age:
            {
                Console.WriteLine("I'M 22 Year old: ");
            }
        }
    }
}





class Program 
{
    static void Main(string[] args)  //main method:
    {
        NAME.Program name = new NAME.Program(); //object of type name:
        NAME.AGE.Program age = new NAME.AGE.Program();   //object of type age.

        name.display();   //calling method to show name:
        age.display();    //calling method to show age:

        Console.ReadKey();
    }
}
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement