Understanding Visual Studio's Default Namespaces
When files for classes and some other types are added to a project, Visual Studio automatically adds a namespace definition. The decision of which namespace to apply is based upon the structure of the project and a simple setting.
When you create a new project using Visual Studio, you are asked for a name for that project. This name is displayed within the Solution Explorer and is also used as the name for the compiled assembly and as the namespace that is applied to new class files when they are added to the project directly. This default behaviour is usually acceptable and can be quite helpful when organising your projects, assemblies and namespaces.
In some situations you may want to change the default namespace for new files. For example, you may have a solution containing multiple projects but where each compiled assembly adds types to a shared namespace. You can change the default namespace, and the assembly name, independently of the name of the project using the project's property pages.
If you are using Visual Studio 2003, right-click the project's name in the Solution Explorer and choose "Properties" from the context-sensitive menu that appears. Select the "General" settings and you will see the two names in the Assembly Name and Default Namespace text boxes.
In newer versions of Visual Studio, right-click the project's name and choose "Properties" from the menu. Ensure that the "Application" tab is selected to see the two settings, pictured below:
The assembly name textbox shows the name that will be applied to the compiled assembly. The actual name have a suffix of ".exe" or ".dll", according to the type of assembly being created. The default namespace specifies the namespace that will be included in new class files added directly to the root of the project. In the above image you can see that the default namespace has been changed to "Test.Application". Adding a class named, "NewClass" gives the following results:
NB: Changing the default namespace only affects newly added files. Existing files that use the old namespace remain unchanged and should be updated manually as required.
To allow you to easily manage the hierarchy of nested namespaces in your projects, Visual Studio allows you to organise your files into folders directly from the Solution Explorer. You can right-click the project or an existing folder and add a new folder using the menu that appears. When you create a class file within a folder, the folder's name is appended to the default namespace, separated with a full stop (period). If the file is added within nested folders, each folder name is added to the default namespace, creating several nested namespaces.
The following sample code shows the effect of adding a class named, "NewClass" to a project with a default namespace of "Test.Application" within the folder, "MyFolder" and the subfolder, "MySubfolder":
14 April 2012