51

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?

5 Answers 5

54

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

Sign up to request clarification or add additional context in comments.

13 Comments

Any hints on creating a new class template and how to achieve it with it?
Sure; File->Export Template->Item Template (then edit it); however, there is an arcane black art to the replacement tokens that you can use. I expect you could use the root template, but it would be hard to pick a namespace for "all the directories except the bottom-most"
At this point, manually dragging a file causes VS2013 to change it's namespace. So they completely negated approach #4. I'm gonna go with #1, I guess.
I like how giving up and changing it manually as I do, is considered a "school of thought"
|
11

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'.

3 Comments

This doesn't answer his question - he's asking how to change the default namespace for a folder within a project, which is difficult, rather than the namespace for a project itself, which is obvious.
Well either way....even after so many years after the post, this is the post that helped me. Thanks!
I agree with AxleWack. I had the same problem, fed it to Google and the 1st result was this thread. So even though it's not an answer for the OP's question, it's still super useful to have this answer around as well. But in order to be fair, the correct answer gets a +1 from me as well.
8

If you have ReSharper installed, right click on the folder in your solution you want to be excluded from namespaces and select properties. In the properties pane, select False for Namespace Provider and that will make Visual Studio ignore the folder when generating namespaces in new files.

Visual Studio solution folder properties pane.

1 Comment

I use ReSharper on Visual Studio 2015, even if I see this option and I always set it to false... it doesn't work. When I create a new class, the namespace ends with the folder name. :( If the new class is a form... the entire solution goes wrong.
3

If you already have one class under that folder with the desired namespace. You could take advantage of the Move type to ClassName.cs light bulb suggestion.

For example, you created Order.cs with the correct namespace MyCompany.MyProject.Domain.Model under the Extensions folder. If you want to add Trace class you do it like so:

enter image description here

Ctrl + . when the edit cursor is anywhere around the class Trace words will show the suggestion.

so... Ctrl + . Down Down Enter

Result:

enter image description here

Comments

2

Resharper has changed how they do this (as of VS2017).

  1. Add your folder.
  2. Add your first class.
  3. Manually alter the namespace to be what you want. (Usually that's removing the last piece from the namespace that represents the folder the class is in.)
  4. The namespace will now be underlined to say that it doesn't match the folder structure. Click on the the options to fix it, and select "Folder '...' do not introduce namespace".

Now your folder is set to expect the namespace for all new classes. However, when you add a new class to the folder, it now complains that it's not the namespace that you set it to in step #3 above.

So, you still need to manually alter the namespace, but at least resharper isn't trying to fight you anymore.

1 Comment

I don't get the underline indicating the namespace doesn't match the folder structure when I change the namespace name.. any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.