Acidic
Acidic

Reputation: 6280

Multiple namespaces in a single project

I find that sometimes I have the need to have multiple namespaces in a project I'm working on - are there any problems that may arise from having multiple namespaces in the same project?
The alternative is obviously having multiple projects (per namespace) in the Solution.

Upvotes: 12

Views: 18068

Answers (3)

Dmitriy Khaykin
Dmitriy Khaykin

Reputation: 5258

The answer to this question, without getting into best practices, is that there won't be any technical problems from using multiple namespaces in one project.

However, visual studio only supports one base namespace, which is located in the project properties (right click properties in solution explorer on the project itself) and any time you create a new file, it will be created with this default namespace (if on root), or the default namespace plus whichever folder structure you have.

Not a problem per se, but you'll need to manually check each new C# file you add to the project and change the namespace accordingly if that particular file will not be using the default namespace for the project.

Upvotes: 1

iefpw
iefpw

Reputation: 7062

Yes many classes to a single namespace. Many namespaces in a project is totally fine. It is cosmetic.

Upvotes: 2

Tim Goodman
Tim Goodman

Reputation: 23986

Yes, it's fine. Often my namespaces align to the folder structure of the project. So the top-level namespace might be the same for the whole project, but there would be multiple sub-namespaces.

The purposes of namespaces are (1) organization and (2) avoiding naming collisions, not necessarily in that order. Whereas, separating things into multiple projects is more because you want multiple binaries or you want to share code between multiple solutions. These are somewhat orthogonal concerns.

Upvotes: 13

Related Questions