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
.
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?
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.
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.
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.
From next time you just need to press “ Ctrl+Shift+W ” to remove unused namespaces and sort them accordingly.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With