AngryHacker
AngryHacker

Reputation: 61646

How to get Visual Studio to use the default namespace when creating a new class?

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?

Upvotes: 0

Views: 1466

Answers (1)

Bradley Uffner
Bradley Uffner

Reputation: 17001

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

Upvotes: 1

Related Questions