Namespaces in C#

Itorian
Posted by in C# category on for Beginner level | Points: 250 | Views : 5226 red flag

In this post you will learn how to write custom namespaces and nested namespaces in C#.

Introduction

Namespaces are C# program elements that allow us to create a system to organize our code. One more very important use, it avoids 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 post we will talk about namespaces:

(i) delimited by . (dot) operator and

(ii) by using ‘using’ directive

Let’s look at example, in console application to print/write line on console we uses ‘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 program.

(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 ‘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 post 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 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 bit simpler.

 

(ii) Using ‘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 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 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.

HAVE A HAPPY CODING!!

Page copy protected against web site content infringement by Copyscape

About the Author

Itorian
Full Name: Abhimanyu Kumar Vatsa
Member Level: Starter
Member Status: Member
Member Since: 4/21/2011 3:54:53 AM
Country: India
Student of M.Tech IT | Microsoft MVP | Mindcracker MVP | IT Faculty | Blogger | Author
http://www.itorian.com
Abhimanyu Kumar Vatsa (Abhimanyu Kumar Singh) is a Microsoft MVP and Mindcracker (C# Corner) MVP (2010, 2011) and having experience in building web scale applications using Microsoft Technologies including ASP.NET, MVC, C#/VB, jQuery, JavaScript, Silverlight, SQL Server and many more web stuffs. He is also handy in non-Microsoft Technologies like PHP, ColdFusion etc but he's specility is always Microsoft. He love to work with Internet scale projects using Microsoft stuffs that has potential to change the every day use of technology. He love to blog and started it around June 2009 and because such blogging Microsoft awarded him MVP.

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 8/11/2011 | Points: 25
Hi Abhimanyu,
Its a good article but can you please elaborate more on nested namespace ?

Thanks and Regards
Akiii
Posted by: Itorian on: 8/11/2011 | Points: 25
@Akiii
Thanks for the lovely comment. Well, nested namespace has no big difference, they are just called using dot notation and using 'using' directives as well.

Login to post response

Comment using Facebook(Author doesn't get notification)