Namespaces
Namespaces in .NET are program elements designed to help you organize your programs. They also provide a means to avoid naming clashes between two sets of code. Implementing Namespaces in your own code is a good idea because it will help save you from problems later on when you want to reuse code you have previously written. For example, if you created a class named Data, you would need to put it in your own namespace to ensure that there wasn't any confusion about when or where the System.Data class should be used. Generally , it would be a bad idea to create a class named Data because it already exists in the .NET Framework Class Library but enclosing it in a namespace certainly would allow you to do it.
Namespaces don't correspond to file or directory names. If naming directories and files to correspond to namespaces helps you organize your code, then you may do so, but it is not required.
Creating a namespace is as simple as putting the namespace key word in front of the name you want to give it and then placing the code that belongs to the new namespace within its braces.
namespace msjc
{
namespace glenn
{
public static void Main()
{
}
}
}
The above example shows how to create a namespace. It is created by putting the word namespace in front of msjc. Inside the msjc namespace a nested namespace has been created called glenn. Notice that each namespace is a block of code. Meaning it is delineated by curly braces. If you want something to be a member of a particular namespace, you put it inside the opening and closing curly braces.
Importing Namespaces
Once you have created a namespace and want to use it in your code you simply use the import statement. For example:
imports System.Collections;
As it turns out this namespace contains the code for the ArrayList. If we didn't import this namespace we would have to qualify the ArrayList name using the dot operator:
System.Collections.ArrayList myArrayList = new System.Collections.ArrayList();
This is a pretty cumbersome thing to do
Creating Regions
Sometimes your code can start getting really big and hard to maintain. VisualStudio has created the region directive that will allow you to define areas within your code. Then you can simply expand the region when you want to work on the code or contract it when you want it out of the way.
The following images show a region I have defined both expanded and contracted: