Stop Visual Studio from putting using directives outside namespace

dav_i picture dav_i · Oct 29, 2014 · Viewed 13.9k times · Source

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
    {
    }
}

Answer

Adam_Dev picture Adam_Dev · Oct 29, 2014

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