Reputation: 1112
I want to add a new namespace to C# project, also number of classes are to be added in that newly created namespace.
When I right-click Solution of Project, I didn't find any link to add a new namespace, similarly by right-clicking Project-Named namespace also.
How to add a new namespace to existing project?
Is it necessary to have a class with Main Method in each namespace?
The project name should be same as namespace name, then how to add new in same project?
Upvotes: 10
Views: 48172
Reputation: 1
Actually, I have just encountered a situation like this in my WPF project. Maybe, this not the exact title for my situation but when I searched it, I could not find any proper solutions for it. Hope it helps others,
Long story short;
I have created a new C# project under my 'Solution'. And I just wanted to use one of the class definitions that was included by the new project. However, even if I add the following snippet to my .xaml code, I was not able to use the 'Path'.
xmlns:Path="clr-namespace:namespace;assembly=namespace"
So what I did was just nothing else but just adding the project as a reference to my Leading project.
Upvotes: 0
Reputation: 24555
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.
Upvotes: 15
Reputation: 2650
How to add a new namespace to existing project? Just add a new folder to your project in which you will put the new classes. The folder name is then part of the namespace: [projectname].[foldername]
Is it necessary to have a class with Main Method in each namespace? Nope, not at all.
The project name should be same as namespace name, then how to add new in same project? By default, the project name is the first part of the namespace. Folders are also part of the namespace, like so: [projectname].[foldername].[subfoldername].[etc]
Note, you can always overwrite a namespace for your classes by just overriding the default generated namespace:
namespace Foo.Bar
{
public class FooBar
{
...
}
}
Upvotes: 3