Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Default Namespace when creating Class in Folder (Visual Studio)

How can I change the default namespace used when you create a new class in Visual Studio?

Background: My solution has a project MyCompany.MyProject.Domain in which I have a folder "Model Base (Linq)" with a subfolder "Extensions" where I keep all partial class extensions.

MyCompany.MyProject.Domain    + Model Base (Linq)         + Extensions            - Order.cs            - Trace.cs  

When I create a new .cs file in there, the namespace gets set to MyCompany.MyProject.Domain.Model_Base\_\_Linq\_\_.Extensions, I only want it to be MyCompany.MyProject.Domain.Model though.

How can I change this default behavior?

like image 989
Alex Avatar asked Aug 23 '09 06:08

Alex


People also ask

How do I change the default namespace in Visual Studio?

Just right click on the name you want to change (this could be namespace or whatever else) and select Refactor->Rename... Enter new name, leave location as [Global Namespace], check preview if you want and you're done!

How do I automatically add a namespace in Visual Studio?

If you are adding a new class in your code then you may need to add the correspondence namespaces. 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

There are 4 schools of thought here:

  • edit it manually
  • live with it
  • change the class template (or export a second class template)
  • create the file outside the sub-directory, then drag it in

I tend to use the 4th; it is quicker than editing, and less hassle than changing the templates

like image 67
Marc Gravell Avatar answered Sep 22 '22 00:09

Marc Gravell


To change the default namespace:

Right click on the project, go to properties and under the 'Application' tab there is a 'Default namespace' field.

Note that this doesn't answer your exact question though, it depends on your folder. You basically you need to rename that folder to 'Model'.

like image 20
Noon Silk Avatar answered Sep 25 '22 00:09

Noon Silk