Namespace can be define as a Collection of classes and Objects which is used to keep separate one set of names from another.The class or Method define in one Namespace will not conflict with class and methods define in another namespace.
System also use a set of Namespace. Like:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
The Using Keyword
The using keyword states that the program is using the names in the given namespace. For example, we are using the System namespace in our programs. The class Console is defined there. We just write:
- Console.WriteLine ("Hello World");
- System.Console.WriteLine("Hello World!");
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace name as follows:
- namespace Namespace_Name
- {
- // code declarations
- }
- namespace_name.item_name;
Method1
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Collections;
- using System.Text;
- using System.Threading.Tasks;
- //NameSpace Demo Start Here
- namespace Demo1
- {
- class Class_Demo1
- {
- public void func1()
- {
- Console.WriteLine("You are in Demo1 Namespace");
- }
- }
- }
- // NameSpace Demo2 Start Here
- namespace Demo2
- {
- class Class_Demo2
- {
- public void func2()
- {
- Console.WriteLine("You are in Demo2 Namespace");
- }
- }
- }
- namespace ConsoleApplication2
- {
- class Program
- {
- public static void Main(string[] args)
- {
- Demo1.Class_Demo1 Obj1 = new Demo1.Class_Demo1();
- Demo2.Class_Demo2 Obj2 = new Demo2.Class_Demo2();
- Obj1.func1();
- Obj2.func2();
- Console.ReadLine();
- }
- }
- }
The Output will be:
You are in Demo1 Namespace
You are in Demo2 Namespace
Method2
We can also avoid prepending of namespaces with the using namespace directive. If we Include the Namespace in program then we can access classes of a Namespace Directly.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Collections;
- using System.Text;
- using System.Threading.Tasks;
- using Demo1;
- using Demo2;
- //NameSpace Demo Start Here
- namespace Demo1
- {
- class Class_Demo1
- {
- public void func1()
- {
- Console.WriteLine("You are in Demo1 Namespace");
- }
- }
- }
- // NameSpace Demo2 Start Here
- namespace Demo2
- {
- class Class_Demo2
- {
- public void func2()
- {
- Console.WriteLine("You are in Demo2 Namespace");
- }
- }
- }
- namespace ConsoleApplication2
- {
- class Program
- {
- public static void Main(string[] args)
- {
- Class_Demo1 Obj1 = new Class_Demo1();
- Class_Demo2 Obj2 = new Class_Demo2();
- Obj1.func1();
- Obj2.func2();
- Console.ReadLine();
- }
- }
- }
Nested Namespaces
Namespaces can be nested where you can define one namespace inside another namespace as follows:
- namespace namespace_name1
- {
- // code declarations
- namespace namespace_name2
- {
- // code declarations
- }
- }
The following program demonstrates this Concept:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Collections;
- using System.Text;
- using System.Threading.Tasks;
- using Demo1;
- using Demo1.Demo2;
- //NameSpace Demo Start Here
- namespace Demo1
- {
- namespace Demo2
- {
- class Class_Demo2
- {
- public void func2()
- {
- Console.WriteLine("You are in Demo2 Namespace");
- }
- }
- }
- class Class_Demo1
- {
- public void func1()
- {
- Console.WriteLine("You are in Demo1 Namespace");
- }
- }
- }
- // NameSpace Demo2 Start Here
- namespace ConsoleApplication2
- {
- class Program
- {
- public static void Main(string[] args)
- {
- Class_Demo1 Obj1 = new Class_Demo1();
- Class_Demo2 Obj2 = new Class_Demo2();
- Obj1.func1();
- Obj2.func2();
- Console.ReadLine();
- }
- }
- }

Join the conversation! Your thoughts help the community grow.