Namespaces
Namespaces are a logical grouping , it is not connected with physical file or code ect. Namespaces are generally structured as hierarchies to use & reuse of names in different contexts . In Microsoft .Net ,a namespace is an abstract container providing context for the items . When we write program , it is created with a default namespace. This default namespace is called as global namespace. But the program has capability of declare any number of namespaces in it also , but it should be a unique name. The result is that every namespace can contain any number of classes, functions, variables and also namespaces etc., whose names are unique only inside the namespace.
In C# , we can define
namespace namespace_name
{
// code
}
For example I am writing names space student , and declaring object in it
namespace student
{
class student_info
{
name string;
roll int;
}
}
Now I , can create instance of class “student_info” ,like
student.student_info
sc = new student. student_info();
namespace class
{
class class_info
{
class_name string;
no_of_student int;
class_teacher_name string;
}
}
Now at the top , we should write
using System;
using classes;
Now class under namespace “classes” , will be easily available .We ca use
class_info c1=new
class_info();
Namespaces allow you to create a systematic organized code via hierarchical system.
A good code should put the general name at the top of the hierarchy and more specific
As get down.
For example “System” is the highest level of namespace hierarchy, “using System” will allow you some general coding , standard library etc.
System.Data, System.Net comes under “System” names space .” System.Data” will allow you to access data adopter, dataset,data reader ect. ” System.Net” for some netword component.Please see the below example
namespace Level1Hierchy
{
class MyClass1
{
//defination of the class
}
namespace Level2Hierchy
{
// Program start class
class MyClass2
{
//defination of the class
}
}
}
“MyClass2” is defined under “Level2Hierchy” , we need to write code to access
Level1Hierchy.Level2Hierchy.MyClass2 obj1 = new Level1Hierchy.Level2Hierchy.MyClass2();
But “MyClass1” is defined under “Level1Hierchy” , we can access by
Level1Hierchy.MyClass1 obj2 = new Level1Hierchy.MyClass1();
No comments:
Post a Comment