31

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
    {
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • 1
    Yes, you can edit the class template; http://stackoverflow.com/questions/2072687/how-do-i-edit-the-visual-studio-templates-for-new-c-sharp-class-interface – Alex K. Oct 29 '14 at 11:36
  • @AlexK. Thanks. Voted to close this as a dupe :) – dav_i Oct 29 '14 at 11:40
  • @dav_i: The answer may be a duplicate - but I'm guessing you didn't know that templates existed? So it may be worth keeping the question based on that fact. – Ian Oct 29 '14 at 11:43
  • Agreed. And thanks for your answer! Closing it as a dupe still keeps the question alive so people can still go `Google>This Q>That Q`. – dav_i Oct 29 '14 at 11:45
  • I was interested to go and read about the StyleCop rule, as I'm used to seeing using directives outside of namespaces (Newtonsoft.Json, NodaTime, Jon Skeet's MiscUtil), and I found these questions which may interest other readers: http://stackoverflow.com/q/125319/397817 http://stackoverflow.com/q/1071797/397817 – Stephen Kennedy Oct 29 '14 at 11:47

6 Answers6

25

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

Kyyshak
  • 113
  • 7
Adam_Dev
  • 259
  • 1
  • 2
  • 2
  • 1
    Hi, thanks for your answer. This setting seems to only work when _adding_ namespaces. Not creating new files. – dav_i Oct 29 '14 at 11:45
  • You can use the templates as suggested above, refer to http://stackoverflow.com/questions/2072687/how-do-i-edit-the-visual-studio-templates-for-new-c-sharp-class-interface – Adam_Dev Oct 29 '14 at 11:54
  • Reset that flag works for ReSharper8.2 and VS2013. Way around. – Lin Song Yang Jun 04 '15 at 06:04
  • In VS2015 and Resharper10 this section is empty and there is nowhere to configure this. – Neutrino Mar 15 '16 at 13:59
  • @Neutrino I can't seem to find this any more. Have you come across anything about this in August? Resharper no longer does the prompting for moving usings inside the namespace and therefore doesn't have the alt+enter combo to do so either. – JARRRRG Sep 01 '16 at 15:03
  • 29
    It's now under Code Editing > C# > Code Style > Reference qualification > Add 'using' directive to deepest scope. – TrueWill Oct 11 '16 at 20:15
  • 3
    Moved again in resharper: It's now under Code Editing > C# > Syntax Style > Add 'using' directive to deepest scope. – ozba Oct 01 '20 at 12:21
  • Downvoted: non-free solution. – Robert Synoradzki Feb 16 '22 at 13:54
17

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

msmolcic
  • 6,407
  • 8
  • 32
  • 56
10

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!

Community
  • 1
  • 1
Ian
  • 33,605
  • 26
  • 118
  • 198
  • 8
    _Generally I don't believe there's any harm in including using statements at the top of your class_ - Compiler will search for directives starting from namespace internals and if not found will continue looking for directives outside of namespace. So - if you put directives outside of namespace and I will create class with same name inside namespace - then "my" class will be used. Putting directives inside namespace is very small effort for adding more safety in your code – Fabio Jul 04 '17 at 09:33
  • 1
    In addition to Fabio's remark, the scope is just different so placing a using directive in-or outside a namespace is difference and can lead to problems. – Steef Mar 19 '21 at 06:52
2

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

Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
1

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

Pang
  • 9,564
  • 146
  • 81
  • 122
ozba
  • 6,522
  • 4
  • 33
  • 40
1

Using .editorConfig you can add the following.

# 'using' directive preferences
csharp_using_directive_placement =inside_namespace:warning
Xavier John
  • 8,474
  • 3
  • 37
  • 51