Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reorder usings and keep them OUTSIDE of the namespace

We would like to Reorder using statements and keep them outside of the namespace. ReSharper puts them inside the namespace when reordering.

Visual Studio or Resharper functionality for placement of using directives asks how to put usings inside the namespace. That is not what we would like to do. Its answer suggest going to ReSharper > Options > Code Editing → C# → Code Style → Add 'using' directive to the deepest scope. Despite having un-selected that, on re-ording the usings, ReSharper places the usings inside the namespace.

enter image description here

How can we Reorder using statements and keep them outside the namespace?

Additional things we have tried:

Our StyleCop.Analyzers ruleset includes the following directive related rules:

SA1200 Using directives must be placed correctly
SA1208 System using directives must be placed before other using directives
SA1209 Using alias directives must be placed after other using directives
SA1210 Using directives must be ordered alphabetically by namespace

Given those rules along with the selection in Options not to "Add usings to the deepest scope", we receive the following warning on build:

SA1200 Using directive must appear within a namespace declaration.

How can we configure ReSharper to enforce that using directives must appear outside a namespace declaration?

like image 745
Shaun Luttin Avatar asked Sep 26 '16 17:09

Shaun Luttin


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 you sort a namespace in C#?

Select Edit from the menu bar. Select Intellisense > Sort Usings. You can also configure different settings for using directives in Tools > Options > Text Editor > C# > Advanced.

What is the purpose of the using directive?

The primary function of the using directive is to make types within a namespace available without qualification to the user code. It considers the set of namespaces and types which are defined in referenced assemblies and the project being compiled.

How do I sort a namespace in Visual Studio?

From next time you just need to press “ Ctrl+Shift+W ” to remove unused namespaces and sort them accordingly.


1 Answers

Add stylecop.json to the project with the following setting:

{  
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "orderingRules": {
      "usingDirectivesPlacement": "outsideNamespace"
    }
  }
}

Then, enable the use of stylecop.json by editing the ProjectName.csproj file, locating the following item in the project file...

<None Include="stylecop.json" />

... and changing the definition to the following.

<AdditionalFiles Include="stylecop.json" />
like image 93
Shaun Luttin Avatar answered Sep 29 '22 17:09

Shaun Luttin