Skip to main content

What is NameSpace in .Net

This a basic question which allways asked in the interview that what is the namespace ,do you know about namespace,can you tell me some words about namespace.

NameSpace is the Logical group of types or we can say namespace is a container (e.g Class, Structures, Interfaces, Enumerations, Delegates etc.), example System.IO logically groups input output related features , System.Data.SqlClient is the logical group of ado.net Connectivity with Sql server related features. In Object Oriented world, many times it is possible that programmers will use the same class name, Qualifying NameSpace with class name can avoid this collision.

Example :-
// Namespace Declaration
using System;


// The Namespace
namespace MyNameSpace
{
    // Program start class
    class MyClass
    {
        //Functionality

    }

}

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. Bellow shows how to create a nested namespace. By placing code in different sub-namespaces, you can keep your code organized.

// Namespace Declaration
using System;

// The Namespaces
namespace MyNameSpace
{
    namespace Video
    {
        // Program start class
        class MyVideo
        {
            //Functionality
            public static void play()
            {
                //..
            }
        }
    }

    namespace Audio
    {
        // Program start class
        class MyAudio
        {
            //Functionality

            public static void play()
            {
                //..
            }
        }
    }
}

Now a question arise on our mind that how to call the namespace members, so there is two way to call namespace members -
  • By implementing fully qualified name inline 
  • By implementing the using directive.
Here i am providing an example of how to call namespace members with fully qualified names.

A fully qualified name contains every element from the namespace name down to the method call.
In the above example there is a nested namespace tutorial within the "MyNameSpace" namespace with class "MyVideo", "MyAudio" and method play().

Here we call this method with the fully qualified name :

public void call()

{
    MyNameSpace.Video.MyVideo.play() 
    MyNameSpace.Audio.MyAudio.play()
}


Now implement namespaces by using directive

using MyNameSpace.Video;
using MynameSpace.Audio;


public void call()
{
  MyVideo.play()
}

If we call "play" method in the same namespace then no need to include "MyNameSpace" at the begin
MyVideo.play()  // calling in the same name space


If our method would not be static then make instance then call by the object of the class.

Popular posts from this blog

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

Scrollable Gridview with fixed headers in asp.net

horizontal and vertical scrollable Gridview with static header row.fixed header in gridview in asp.net.static header in gridview through java script.I was looking for a solution for this for a long time and found most of the answers are not working or not suitable for my situation i also find most of the java script code for that they worked but only with the vertical scroll not with the horizontal scroll and also combination of header and rows doesn't match.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview, Scrollble gridview with fixed header through javascript, File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview.

We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)