Tuesday, 18 December 2012 17:55

c# namespaces

Written by 
Rate this item
(0 votes)

namespaces is a old concept in c# programming.both
java and c# uses this concept.a namespace is act like
a 'wrapper' around one or many elements which gives
them uniqueness.

As a example a namespace can be
definded as follows-
namespace Mysite{
public class Mysite1{{enum enum1}}}

why namespaces is necessary? for one thing,it avoids
the name collision.Problem can come when you going to
use many third party libraries .all of them use
same named classes.as a example, suppose yuo have a
utility library,application library,other libraries,
every of them may implement a linked list class-LinkedList.
Now if you write-
LinkedList object=new LinkedList();

then how the compiler or the linker will know
which linked list class you wrote?
to avoid the problem you have to use namespaces
to identify that the LinkedList class is a version
of the specific library like utility library by the
following way-
utility.LinkedList=new utility.LinkedList();

following is a full example of namespaces-

namespace ExampleNamespace{
public class ExampleClass{
int value;
public ExampleNamespace(){ value = 0; }
public int getTheValue(){ return value;}

class Program{
public static void main(){ExampleNamespace.ExampleClass
foo= new ExampleNamespace.ExampleClass();

the one more worth mentioning on namespace is that they
use the 'using' keyword to tell the compiler about what
libraries and namespaces of code you are gonna use
within the system.a example follows-
//call the console class of the system
console.writeline("hello viewers");
also the 'using' keyword is used to create aliases
for namespaces.aliases is needed when one have
multiple namespaces having common method or when
there are a long list of nested namespaces
arising a long name.

Read 2107 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments