SIGN UP MEMBER LOGIN:    
ARTICLE

Namespaces in C#

Posted by Abhimanyu Kumar Vatsa Articles | C# Language August 10, 2011
In this article you will learn how to write custom namespaces and nested namespaces in C#.
Reader Level:

Introduction

Namespaces are C# program elements that allow us to create a system to organize our code. One more very important use is to avoid name clashes between two sets of code. Using namespaces in our code is a good habit because it is likely to save us from problems later when we want to reuse some of our code.

In this article we will talk about namespaces.

(i) delimited by . (dot) operator and

(ii) by using 'using' directive

Let's look at an example; in a console application to print/write a line on the console we use 'Console.WriteLine("Hello");'. This is only possible, if you use 'using System' directive in code. What is 'System' here? That's the namespace. Let's look at the ways to use namespaces in programs.

(i) Dot Operator

class Program
{
    static void Main(string[] args)
    {
        System.Console.WriteLine("Using dot operator.");
        System.Console.ReadKey();
    }
}

/* output:-
Using dot operator.
*/

(ii) Using the "using" directive
 
using System;
 
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Using 'using' directive.");
        Console.ReadKey();
    }
}
 
/* output:-
Using 'using' directive.
*/

Look at the difference in both programs; you will notice the use of dot operator and using directive.

In this article we are going to create own namespaces.

(i) Using dot operator

Let's look at the way to use dot operator in C#.

Program1.cs

using System;

namespace demo
{
    class Program1
    {
        public static void myMethod1()
        {
            Console.WriteLine("Hello demo Namespace using dot operator.");
        }
    }
}

Program.cs (From below, anyone may be used)

 

using System;

 

namespace demo

{

    class Program

    {

        static void Main(string[] args)

        {

            demo.Program1.myMethod1();

            Console.ReadKey();

        }

    }

}

 

/* output:-

Hello demo Namespace using dot operator.

*/

using System;

 

class Program

{

    static void Main(string[] args)

    {

        demo.Program1.myMethod1();

        Console.ReadKey();

    }

}

 

/* output:-

Hello demo Namespace using dot operator.

*/

In the above programs, I have organized the same program in two different C# files. In the first program, I have used a namespace 'demo' and inside this I have a class 'Program1' and inside this a method "myMethod1()". Now to use "myMethod1()" method in Main, I have used its fully qualified path like:

namespace_name.class_name.myMethod1();

Actually, this way is not much useful when we need to call myMethod1() many-times. Let's re-write Program.cs code a bit simpler.

(ii) Using the "using" directive

Program.cs

using System;
using demo;
 
class Program
{
    static void Main(string[] args)
    {
        Program1.myMethod1();
        Console.ReadKey();
    }
}

/* output:-
Hello demo Namespace using dot operator.
*/

Please note, in the above program I am using just "class_name.myMethod1()" instead of fully qualified name "namespace_name.class_name.myMethod1()". And for this I am using a new namespace "using demo;" at the top.

Nested Namespace

Namespaces can be nested as well. A good way to organize our namespaces is via a hierarchical system. We put the more general names at the top of the hierarchy and get more specific as we go down. This hierarchical system can be represented by nested namespaces. Look at the program, which has a nested system.

using System;

namespace demo
{
    namespace test
    {
        namespace itorian
        {

            class Program

            {
                static void Main(string[] args)
                {
                    Console.WriteLine("Welcome to Nested Namespace.");
                    Console.ReadKey();
                }
            }
        }
    }
}

/* output:-
Welcome to Nested Namespace.
*/

So, that's all about the namespaces (custom namespaces) in C#. I hope you like my effort here.

Login to add your contents and source code to this article
share this article :
post comment
 

Hello sir Very good consulting topics for the beginners in c# language. Thanks & Regards Ram Mehar Noida

Posted by rammehar mehar Jan 29, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor