Namespace
In C#, namespaces are the group of classes, structs, interfaces, enums and delegates. In simple word can say that It provide one set of name different from other set of name. The biggest advantage of namespace the class name witch are declare in one namespace will not clash to same class name declare in other namespace. The namespaces in C# can be nested. That means one namespace can contain other namespaces also. The .NET framework already contains number of standard namespaces like System, System.Net, System.IO etc. if we access the namespace in our program then use the “using’ keyword. It is not possible to write the any access specifier (public, private, protected etc) with the declaration of namespace. The namespace are by default public access. Namespace elements can’t be declare private or protected. Namespace allow only public elements
Like this –
using namespace-name;
Declaring a namespace
In c# language provide a keyword “namespace” for creating the user defined datatype.
Syntax
namespace namespace-name{
// declare the namespacess
// declare the classes
// declare the structures
// declare the enums
// declare the delegates
}
name of namespace can be a valid name or combination of variable separated by comma.
Namespace Computer{
//declare the class, structure and enum
}
or
namespace Computer.cpu.moniter{
//declare the class, structure and enum
}
Example of namespace
namespace Mind
{
class MyClass
{
}
}
This code doesn’t compile because internal class is private so we know that Namespace allow only public element
namespace Mind
{
private class MyClass
{
}
}
Accessing namespace member
The namespace members can be access by full name of namespace witch include namespace and member with separate by dot (.) operator.
using System;
namespace Vahicle.Car{
// declare the class
class Oddy{
public Oddy()
{
Console.WriteLine(“Oddy car”);
}
}
}
class Customer{
public static void Main()
{
Vahicle.Car.Oddy oddy = new Vahicle.Car.Oddy();
}
}
If we type the fully qualified name then taken long time so this is the bad things, if do not write the fully qualified name then c# provide a keyword named as “using” at the top program like this.
Syntax.
using [namespace_name][.][sub-namespace_name];
using System;
using Vahicle.Car; // using keyword
class Customer{
public static void Main()
{
Oddy oddy = new Oddy();
}
}
We are use the “using” keyword inside the namespace.
namespace MyNameSpace
{
using System; // “using” keyword inside the namespace
class MyClass
{
}
}
If we don’t write the all classes, interface, structure, enum, and delegates in a single namespace so we can span the namespace in single program like this.
Using System;
Namespace Company {
Class MyClass{
Public MyClass(){
Console.WriteLine(“My class”);
}
}
}
Namespace Company {
Class Client{
Public staic void Main()
{
MyClass mcl=new MyClass();
}
}
}
Nested namespace
We can also define the namespace into another namespace this termed is called nested namespace. If we want to access the member of nested namespace user has to use the dot (.) operator.
Like this –
System.Collections.Generic
using System;
using Main.Nested;
// main namespace
namespace Main{
// nested namespace
namespace Nested {
// class declaration
class NestedDemo {
// constructor
public NestedDemo()
{
Console.WriteLine('Demo of Nested namespace');
}
}
}
}
public class Program
{
public static void Main()
{
// three type of access the nested namespace
Main.Nested.NestedDemo main=new Main.Nested.NestedDemo();
new Main.Nested.NestedDemo();
NestedDemo nd=new NestedDemo() ;
}
}
Output
Demo of Nested namespace
Demo of Nested namespace
Demo of Nested namespace
Creating Aliases
It is possible to create a sort name of fully qualified name of namespace .
Syntax
Using alias-name = [namespace_name][.][sub-namespace_name];
using output=System.Console;
public class Program
{
public static void Main()
{
output.WriteLine('Hello World');
}
}
Leave Comment