Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Visual Studio from putting using directives outside namespace

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     {     } } 
like image 655
dav_i Avatar asked Oct 29 '14 11:10

dav_i


People also ask

Should using directives be inside or outside the namespace?

As a rule, external using directives (System and Microsoft namespaces for example) should be placed outside the namespace directive. They are defaults that should be applied in all cases unless otherwise specified.

How do I automatically add a namespace in Visual Studio?

If you are adding a new class in your code then you may need to add the correspondence namespaces. To do it, you may either manually type the Using Namespace or you just right click on the class name and select Resolve > Namespace. But using “Ctrl+.” you can automatically add the namespace in your code.

What is namespace in c# net?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types. C# Copy.


1 Answers

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

like image 123
Adam_Dev Avatar answered Oct 07 '22 11:10

Adam_Dev