Stop Visual Studio from putting using directives outside namespace

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


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!


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


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