Name Spaces in
Name Spaces in
Name Spaces
What is a namespace? A Namespace in Microsoft .Net is like containers of objects. They may contain unions, classes, structures, interfaces, enumerators and delegates. Main goal of using namespace in .Net is for creating a hierarchical organization of program. In this case a de eloper does not need to !orry about the naming conflicts of classes, functions, ariables etc., inside a project. In Microsoft .Net, e ery program is created !ith a default namespace. This default namespace is called as global namespace. "ut the program itself can declare any number of namespaces, each of them !ith a uni#ue name. The ad antage is that e ery namespace can contain any number of classes, functions, ariables and also namespaces etc., !hose names are uni#ue only inside the namespace. The members !ith the same name can be created in some other namespace !ithout any compiler complaints from Microsoft .Net. Namespaces are a !ay to define the classes and other types of information into one hierarchical structure. System is the basic namespace used by e ery .N$T code. If !e can e%plore the System namespace little bit, !e can see it has lot of namespace uses the system namespace. For example System.Io, System.Net, System.&ollections, System.Threading, etc. A namespace can be created ia the Namespace key!ord. 'ere is an e%ample to create ( Boo!s( namespace in )".N$T and &*. VB.NET "ode Namespace "ooks &lass Authors +,o something $nd &lass $nd Namespace "# "ode namespace "ooks class Authors ..,o something / / "reating and using Namespaces in VB.NET and "# This is simple namespace e%ample. 0e can also build hierarchy of namespace. 'ere is an e%ample for this. VB.NET "ode Namespace "ooks Namespace In entory Imports System &lass AddIn entory 1ublic 2unction MyMethod34 &onsole.0rite5ine3(Adding In entory ia MyMethod6(4 $nd 2unction $nd &lass $nd Namespace $nd Namespace
Page 1
11/19/2013
Visual Studio.NET Best Articles "# "ode using System7 namespace "ooks namespace In entory class AddIn entory public oid MyMethod34 &onsole.0rite5ine3(Adding In entory ia MyMethod6(47 / / / / That+s all it takes to create namespace. 5et+s look ho! !e can use the namespaces in our code. I+m going to create a standalone program to use the namespaces. VB.NET "ode Imports System &lass 'ello0orld 1ublic Sub Main34 ,im AddIn As In entory.AddIn entory 8 Ne! AddIn entory AddIn .MyMethod34 $nd Sub $nd &lass $% Imports System.In entory &lass 'ello0orld 1ublic Sub Main34 ,im AddIn As AddIn entory 8 Ne! AddIn entory AddIn .MyMethod34 $nd Sub $nd &lass
"# "ode using "ooks7 class 'ello0orld public static oid Main34 In entory.AddIn entory AddIn 8 ne! AddIn entory347 AddIn .MyMethod347 / / $% using "ooks.In entory7 class 'ello0orld public static oid Main34 AddIn entory AddIn 8 ne! AddIn entory347 AddIn .MyMethod347
Page 2
11/19/2013
Note 0hen using Imports statement or 9sing statement !e can use only the namespace names, !e can+t use the class names. 2or e%ample, the follo!ing statements are in&alid. Imports "ooks.In entory.AddIn entory using "ooks.In entory.AddIn entory7 Final Notes The namespaces are building blocks for the .N$T !ay of soft!are de elopment. The namespaces !ide opens the doors for :+d party and project specific custom components and class libraries.
Page 3
11/19/2013