Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new Namespace in C# Project

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?

like image 555
maliks Avatar asked May 04 '16 11:05

maliks


People also ask

Can you have more than one namespace in C#?

Two classes with the same name can be created inside 2 different namespaces in a single program. Inside a namespace, no two classes can have the same name.

How do you namespace in C#?

In a simple C# program, we use System. Console where System is the namespace and Console is the class. To access the class of a namespace, we need to use namespacename. classname.

Is namespace required in C#?

There is no need to have a namespace. However developer studio expects you to be using a name space. For example, when you choose to add a class to a project developer studio will: Create a file for the class.

How do I automatically add a namespace in Visual Studio?

To do it, you may either manually type the Using Namespace, or you just right click on the class name and select Resolve > Namespace. But using “Ctrl+.” you can automatically add the namespace in your code.


2 Answers

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.

like image 158
David Pine Avatar answered Oct 23 '22 12:10

David Pine


  • 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
    {
        ...
    }
}
like image 40
ZiNNED Avatar answered Oct 23 '22 14:10

ZiNNED