Before answer each of the questions, I wanted to shared this from MSDN:
The namespace
keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.
Question 1
How to add a new namespace to existing project?
When you create a class you can place it in nearly any namespace you'd like, it can be made up entirely or one that already exists.
namespace Something.That.No.One.Else.Has
{
public class FooBar() { }
}
Now, this Something.That.No.One.Else.Has
namespace is available throughout my application. And I need to use the using
statement in order to consume the FooBar
class, unless the consuming class is in the same namespace.
Question 2
Is it necessary to have a class with Main Method in each namespace?
No, there is no requirement built into the language that would impose this. You're probably thinking of static void Main
for .exe
.
Question 3
The project name should be same as namespace name, then how to add new in same project?
As I mentioned in answer 1, you can simply create a namespace
as you see fit. I would suggest that for organizational reasons to start a new project though as it will likely be confusing for anyone who starts working in your source.
When you create a new project the naming convention followed is to set the default namespace of the project to the name of the project.
Update
After hearing from the OP, I think I have a better understanding of the dilemma.
In your project you should create a folder structure that is relevant to the relationship of the files and their respective implementations. Following recommended practices and patterns from Microsoft.
<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]
.
+-- Graphs
| +-- SomeGraphClass.cs // in CompanyName.Product.Graphs namespace
| +-- ...
+-- Data
| +-- SomeDataClass.cs // in CompanyName.Product.Data namespace
| +-- ...
+-- Interfaces
| +-- IFooBar.cs // in CompanyName.Product.Interfaces namespace
| +-- ...
Update 2
The folder structure (by default) dictates the namespace
of the class. For example, when you right-click on the folder and Add > New Item > Class
- it will create a class in that folder but it will give it the namespace
as described.