Microsoft > CSharp >> Namespace Views : 6642
Rate This Article :

Namespace and using Keyword

Namespace

Namespace is a collection of types

A namespace can contain one or more of the following types: class, struct, interface, enum and delegate

A namespace can have one or more sub namespaces defined in it.

Namespace is used to segregate types defined in a program

Namespace also avoids collision between names of types in a program.

Namespace performs logical segregation of types in a program. That is the types are present in the same program but can be resolved only with the help of namespace that contains them.

By default a program has a namespace called <global namespace>

 using keyword

using keyword is used to resolve a type for its namespace

When a type is referred by a code in a program the compiler searches for the type in the namespace in which the code is present. If a type is found then it is used for the code.

 

                   class One { }       // class 'One' in <global namespace>

 

                   class class1

                   {                 

                       static void Main(string[] args)

                       {

                           One obj = new One(); // code in <global namesapce>

                       }   

                   }

 

Both the code and the class are present in the same namespace, hence object of the class ‘One’ will be created by the code.

          If no type is found in the namespace of the code then a compile time error is displayed.

                    namespace Sample

                   {

                       class One { }     // class 'One' in <Sample       namespace>

                   }

         

                   class class1

                   {

                       static void Main(string[] args)

                       {

                           One obj = new One(); // code in <global namesapce>

                       }   

              }

     If using directive is present in the namespace of the code, then the compiler searches for the type to be resolved in the namespace mentioned in the using directive. If a type is found then it is used for the code.

           using Sample;           // using section in <global namespace>

           namespace Sample

          {

              class One { }       // class 'One' in <Sample namespace>

          }

         

          class class1   

          {

              static void Main(string[] args)

              {

                  One obj = new One(); // code in <global namesapce>

              }   

          }

      In the above program an object of type One is successfully created by the code. 

About Author
Jayanthan JVP
Total Posts 16
DotNet Developer
Comment this article
Name*
Email Address* (Will not be shown on this website.)
Comments*
Enter Image Text*
   
View All Comments
boydx
10qqqqqq
  Privacy   Terms Of Use   Contact Us
© 2016 Developerin.Net. All rights reserved.
Trademarks and Article Images mentioned in this site may belongs to Microsoft and other respective trademark owners.
Articles, Tutorials and all other content offered here is for educational purpose only and its author copyrights.