0

When I create a new class in a sub folder in my project, by default it uses DefaultNameSpace.SubFolder.

Anyway to make it just use the default namespace defined at the project level?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • Possibly this is already addressed here? http://stackoverflow.com/questions/2871314/change-project-namespace-in-visual-studio – Michael Armes Jan 08 '16 at 19:48

1 Answers1

1

You would have to create a new template for Class. You can use the existing template and just remove the extra "fluff" on the namespace.

See this answer: How do I edit the Visual Studio templates for new C# class/interface?

The specific file to modify for your version of Visual Studio is:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

You may want to make a copy of that structure and give your template a different name so you don't end up breaking the default template if you ever want to go back to it.

Edit

Looking at the tokens now, I don't see one specifically for the Default Namespace, $safeprojectname$ is probably as close as you get get using tokens, or just hard code it for your specific template. This is what the template would look like:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $safeprojectname$
{
    class $safeitemrootname$
    {
    }
}

You can find the predefined tokens here: https://msdn.microsoft.com/en-us/library/eehb4faa.aspx

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • This MSDN link will guide you through exporting an existing template, modifying it, and using it in VS. https://msdn.microsoft.com/en-us/library/ms185319.aspx – Bradley Uffner Jan 08 '16 at 20:02
  • 1
    As of VS 2022, there is now a replaceable parameter named, '$defaultnamespace$' which is what you want. Despite the information on MSDN (in several places) '$safeprojectname$' no longer seems to be available for item templates. – krowe Mar 19 '23 at 10:40