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;
we can access all the classes and Method of a Namespace through “Using” Keyword.
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");
We could have written the fully qualified name as if we are not using a Namespace :
- System.Console.WriteLine("Hello World!");
Now we Understand Some user Define Namespaces. How can we create userdefine Namespaces:
Defining a Namespace A namespace definition begins with the keyword namespace followed by the namespace name as follows:
- namespace Namespace_Name
- {
-
- }
To call the namespace-enabled version of either function or variable, prepend the namespace name as follows:
- namespace_name.item_name;
The following program demonstrates use of namespaces:
Method1 - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Collections;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Demo1
- {
- class Class_Demo1
- {
- public void func1()
- {
- Console.WriteLine("You are in Demo1 Namespace");
- }
- }
- }
-
-
- 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 Demo1
- {
- class Class_Demo1
- {
- public void func1()
- {
- Console.WriteLine("You are in Demo1 Namespace");
- }
- }
- }
-
-
-
- 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
- {
-
- namespace namespace_name2
- {
-
- }
- }
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 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 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();
- }
- }
- }