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

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.

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.

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)

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.