Stop Visual Studio from putting using directives outside namespace

15,869

Solution 1

Generally I don't believe there's any harm in including using statementents at the top of your class. I actually find it easier to include them there, so it's up to you whether you want to respect that rule.

If you do however, all of the file templates are available and can be edited. See the answer How do I edit the Visual Studio templates for new C# class/interface? to detail where they live on each Visual Studio version.

Once you're there you can change the layout, so for example a basic class looks 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 $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

You could change this to the following or similar:

namespace $rootnamespace$
{
    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$

    class $safeitemrootname$
    {
    }
}

There may be quite a few files to change though!

Solution 2

You can set this in Re-sharper.

Re-sharper > Options > C# > Namespace Imports > Add using directive to the deepest scope.


Update: As of VS2015 and Resharper10, this has moved. It is now under:

Code Editing > C# > Code Style > Reference qualification > Add 'using' directive to deepest scope

Solution 3

There is an easier way to do it nowadays without modifying templates and without ReSharper. Ever since Visual Studio 2019 update 16.1 was released you can setup the usings inside the namespace by following these steps:

  1. Open Visual Studio
  2. In the toolbar, go to Tools
  3. Within the Tools menu, open Options
  4. In the options list, select the Text Editor > C# > Code Style > General
  5. Scroll down near the bottom, find the 'using' preferences section and set the Preferred 'using' directive placement to Inside namespace

enter image description here

Solution 4

There's also a Visual Studio extension for VS 2015 and 2017 that fixes using declarations after the fact. It also has some configuration options for grouping particular declarations (like System.*)

https://marketplace.visualstudio.com/items?itemName=everfor88.UsingDirectiveFormatter

enter image description here

Solution 5

In ReSharper 2020, it's under Code Editing > C# > Syntax Style > Add 'using' directive to deepest scope.

Share:
15,869
dav_i
Author by

dav_i

Multi-language developer with extensive experience in C# and Kotlin, with a test-driven mindset, and a passion for finding simple solutions to complex problems. My goal is to solve novel problems to help the world be a better place.

Updated on June 15, 2022

Comments

  • dav_i
    dav_i about 2 years

    Is there a setting in Visual Studio (or ReSharper) that allows you to specify what namespaces should be default and which scope they are put in?

    The default for an MVC project for example is

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Namespace
    {
        public class Class1
        {
        }
    }
    

    but ReSharper and StyleCop complain:

    All using directives must be placed inside of the namespace. [StyleCop Rule: SA1200]
    Using directive is not required by the code and can be safely removed

    Is there a way to make the default simply:

    namespace Namespace
    {
        public class Class1
        {
        }
    }