Download - 6)Namespace
-
8/17/2019 6)Namespace
1/6
Introduction To Namespaces :
Microsoft’s V isual Studio .NET has i ntroduced many new concepts t o the Visual Studiodeveloper, including the Microsoft Intermediate Language (MSIL) with runtime compilation,garbage collection, Common Language Runtime (CLR), and, perhaps m ost misunderstood of all:namespaces an d a ssemblies. To help yo u d evelop a b etter understanding of the .NETenvironment, this article will explore namespaces and assemblies and clarify the relationshipbetween them.
The namespaceAt rst glance, it appears a s i f namespaces r epresent little more t han the C++ include d irective orthe addition of a VB module to a p roject. But the concept of namespaces a nd assemblies withthe C# using d irective and the VB Imports st atement in Visual Studio .NET extends beyond theinclusion of predened header les. They represent a method of interacting with external codelibraries t hat may be new to the Microsoft developer.
Put simply, a namespace is j ust a grouping of related classes. It's a method of putting classesinside a container so that they can be clearly d istinguished from other classes w ith the samename. Programmers ski lled in the Java language will recognize n amespaces as packages. Anamespace is a logical grouping rather than a physical grouping. The physical grouping isaccomplished by an assembly, which equates most directly to a dynamic link library (DLL), COMobject, or OCX module.
The .NET CLR consists of multiple n amespaces, which a re sp read a cross m any asse mblies. Forexample, ADO.NET is t he set of classes l ocated in the System.Data n amespace, and ASP.NETis t he set of classes located in the System.Web namespace.
Figure A show s how classes ar e divided up in the namespaces t hat compose t he .NET CLR.Each block represents a separate n amespace. In the CLR, the classes a nd structures containedin each of the namespaces r epresent a common theme of development responsibility. Further,the namespace system can be hierarchical, allowing for compartmentalization of functionalityinside of a parent namespace. For exam ple, the System namespace is t he root for al l othernamespaces.
Figure A
-
8/17/2019 6)Namespace
2/6
Namespace Implementation
Namespaces in C# is a method designed for organising classes.Other than classes thenamespace can have
• Structures
• Interfaces
• Enumerations
• Delegates
A namespace is similar to a package in Java !hereas Java package names representdirector" structure !hile C# namespaces represent onl" the logical structure. he" alsohelp in avoiding clashes $et!een similar named files. Namespace act as $oundar"$et!een these t!o files.Suppose a piece of code using t!o of them ho! could "oudifferetiate $et!een t!o% ofcourse the Namespace is the ans!er.ImplementingNamespaces in "our o!n code is a good practise since one could use the same name ofthe classes kno!ingl" or unkno!ingl" !ithout getting into an" trou$le. usingNamespace allo! "ou to use mem$ers of the namespace .
&sing Namespace
Namespaces are referenced $" C# applications $" using the using ke"!ord. 'or instance
&sing S"stem.Collections.(eneric)
S"stem namespace gives access to the interfaces and classes that define genericcollections.Interface such as I*ist allo! one to override its method Add. he S"nta+ ofAdd,
void Add- item
-
8/17/2019 6)Namespace
3/6
he Using Directive
E+ample. DirectiveE+.cs
using S"stem) using DotNetpro/ect.CSharp)
class DirectiveE+0
pu$lic static void 1ain-0
2rint e+t.te+tString- )3
3 namespace DotNetpro/ect.CSharp)
0 class 2rint e+t 0 pu$lic static void te+tString-
0 Console.4rite*ine-5E+ample Demonstrating Namespace5 ) 3 3 3
he using directive DotNetproject.CSharp allo!s us to call mem$ers ofthe DotNetproject.CSharp namespace !ithout using the full" 6ualified name. his ena$leus to call PrintText.textString() directl". Other!ise !henever !e !ant to call thatmethod textString !e need to !rite long full" 6ualifiedname DotNetproject.CSharp.PrintText.textString() .
Using Keyword:
he using ke"!ord provides a convenience mechanism to correctl" use IDisposa$leo$/ects. As a rule !hen "ou use an IDisposa$le o$/ect "ou should declare andinstantiate it in a using statement. he using statement calls the Dispose method on theo$/ect in the correct !a" and -!hen "ou use it as sho!n earlier it also causes theo$/ect itself to go out of scope as soon as Dispose is called. 4ithin the using $lock theo$/ect is read7onl" and cannot $e modified or reassigned.
he follo!ing e+ample sho!s ho! to use the using statement.C#using -'ont font8 9 ne! 'ont- 5Arial5 8:.:f0 $"te charset 9 font8.(diCharSet)3
;emarks
'ile and 'ont are e+amples of managed t"pes that access unmanaged resources -in thiscase file handles and device conte+ts . here are man" other kinds of unmanagedresources and class li$rar" t"pes that encapsulate them. All such t"pes must implementthe IDisposa$le interface.
http://void%280%29/http://msdn.microsoft.com/en-us/library/system.io.file.aspxhttp://msdn.microsoft.com/en-us/library/system.drawing.font.aspxhttp://msdn.microsoft.com/en-us/library/system.idisposable.aspxhttp://msdn.microsoft.com/en-us/library/system.io.file.aspxhttp://msdn.microsoft.com/en-us/library/system.drawing.font.aspxhttp://msdn.microsoft.com/en-us/library/system.idisposable.aspxhttp://void%280%29/
-
8/17/2019 6)Namespace
4/6
As a rule !hen "ou use an IDisposable o$/ect "ou should declare and instantiate it ina using statement. he using statement calls the Dispose method on the o$/ect in thecorrect !a" and -!hen "ou use it as sho!n earlier it also causes the o$/ect itself to goout of scope as soon as Dispose is called. 4ithin the using $lock the o$/ect is read7onl"and cannot $e modified or reassigned.
he using statement ensures that Dispose is called even if an e+ception occurs !hile"ou are calling methods on the o$/ect.
-
8/17/2019 6)Namespace
5/6
he Namespace Alias ualifier in C# lets developers use the alias name instead of the
complete namespace name. he advantage of the Namespace Alias ualifier is that it
lets "ou use the alias name instead of a $igger namespace - like Inner Namespaces
and it also helps to avoid the am$iguous definitions of the classes.
'or e+ample
ake the $elo! class as an e+ample
vie! sourceprint %:8. using S"stem):B. using S"stem.Collections.(eneric):>. using S"stem.*in6):?. using S"stem. e+t)
: . using S"stem. hreading. asks):F. :G. namespace Namespace8:H. 0: . class Student8:. 088. pu$lic string StudentName 0 get ) set ) 38B. 38>. 38?. namespace NamespaceB8 . 08F. class Student8G. 08H. pu$lic string StudentName 0 get ) set ) 38 . 3B:. 3
4hen $oth Namespace8 and NamespaceB are referenced in the C# file and are tr"ing to
create an instance of Student it !ill cause the Am$iguous-Samename K error.
Global Name Alias:
8. namespace est1odule
B. 0
>. class estClass
?. 0
. class est1odule 0 3
F. est1odule. estClass test 9 new est1odule. estClass- )
G. 3
H. 3
http://dotnet.dzone.com/articles/namespace-alias-qualifier-c#viewSourcehttp://dotnet.dzone.com/articles/namespace-alias-qualifier-c#printSourcehttp://dotnet.dzone.com/articles/namespace-alias-qualifier-c#abouthttp://dotnet.dzone.com/articles/namespace-alias-qualifier-c#viewSourcehttp://dotnet.dzone.com/articles/namespace-alias-qualifier-c#printSourcehttp://dotnet.dzone.com/articles/namespace-alias-qualifier-c#about
-
8/17/2019 6)Namespace
6/6
4hen "ou tr" to instantiate estClassK 6ualified !ith the namespace name est1oduleK
the compiler !ill give an error sa"ing, he t"pe name L estClassM does not e+ist in the
t"pe L est1odule. estClass. est1oduleMK. his happens $ecause the class est1oduleK
!hich is inside hides the namespace est1oduleK !hich is at the glo$al level.
he e+ternal alias global -C# @Global - .NE can $e used to resolve conflicts $et!een
namespaces and hiding inner t"pes as sho!n $elo!,
vie! plain
8. namespace est1odule
B. 0
>. class estClass
?. 0
. class est1odule 0 3
F. glo$al,, est1odule. estClass test 9 new glo$al,, est1odule. estClass- )
G. 3
H. 3
4hen the global K alias is used the search for the right7side identifier starts at the
glo$al namespace !hich is the root level of the namespace hierarch". his helps to
resolve the t"pe from the outer most level -glo$al level drilling in!ards.
he chances for such conflicts can $e avoided to a good e+tent $" follo!ing proper
naming standards. ut !hen huge class li$raries and third7part" components are used
this situation can arise. In such cases the usage of namespace alias 6ualifier can save a
lot of time==
http://www.coderewind.com/2012/08/how-to-use-namespace-alias-qualifier-global/http://www.coderewind.com/2012/08/how-to-use-namespace-alias-qualifier-global/