Monday, 3 October 2016

C# Namespaces


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();

We can use “using” keyword also to access the namespace. This statement tells the compiler that the subsequent code is making use of names in the that namespace.For example I am creating a namespace “class” and a class of name “class_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();

 Namespace hierarchy
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
        }
    }
}

“Level1Hierchy” is the top level namespace , “Level2Hierchy” is under Level1Hierchy.
“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

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...