Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Namespaces in C#


A Namespace is simply a logical collection of related Classes in C#. We bundle our related classes (like those related with database activity) in some named collection calling it a namespace (e.g., DataActivity). As C# does not allow two classes with the same name to be used in a program, the sole purpose of using namespaces is to prevent name conflicts. This may happen when you have a large number of classes, as is the case in the Framework Class Library (FCL). It is very much possible that our Connection Class in DataActivity conflicts with the Connection Class of InternetActivity. To avoid this, these classes are made part of their respective namespace. So the fully qualified name of these classes will be DataActivity.Connection and InternetActivity.Connection, hence resolving any ambiguity for the compiler.



So, in the second line of our program we are declaring that the following classes (within { } block) are part of
MyHelloWorldApplication namespace.





namespace MyHelloWorldApplication
{
...
}




  • The C# namespaces have NO physical mapping as is the case in Java. Classes with same namespace can be in different folders.

  • The namespace may contain classes, events, exceptions, delegates and even other namespaces called Internal namespace.


These internal namespaces can be defined like this:





namespace Parent
{
namespace Child
{
...
}
}
http://codesforprogrammers.blogspot.com/feeds/posts/default?alt=rss


This post first appeared on Codes For Programmers(C4P), please read the originial post: here

Share the post

Namespaces in C#

×

Subscribe to Codes For Programmers(c4p)

Get updates delivered right to your inbox!

Thank you for your subscription

×