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();
console.write("ExampleClass.Value={0}",foo.getTheValue()
);}
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.