Skip to content

using-order

C#cleanupfix: safedefault: offcsharp/stylecop

Reports using directives of a file or namespace that are out of order: System first (system_first), names in alphabetical order (compare), and, with the options static_position and alias_position set to after, using static directives after the plain ones and aliases after those, each group sorted. global using and using unsafe stay where they are, and a list restarts at a directive that has a preprocessor directive in front of it (the fix leaves such lists alone). Comments on lines of their own stay where they are; a comment after a directive moves with it.

A sorted list is easy to scan for a namespace, and two developers who add a directive do not both put it at the end and conflict. The System first convention is that of the .NET libraries.

Reported
using Zeta;
using static Foo.Bar;
using Alpha;
using X = Y.Z;
using System;
using static System.Math;
using Aa;
using aa;
...
After the fix
using System;
using static Foo.Bar;
using Aa;
using X = Y.Z;
using Alpha;
using static System.Math;
using Zeta;
using aa;
...
csharp:
lint:
rules:
using-order:
level: warn # off (default), warn, error
options:
system_first: true
compare: "ordinal"
static_position: "keep"
alias_position: "keep"
Option Type Default Description
system_first bool true Put System and System.* before the other namespaces.
compare string "ordinal" ordinal compares code points; culture ignores case (and puts punctuation before digits before letters, a lower-case name before an upper-case one that is otherwise equal); culture_ignore_case leaves names that differ only in case in their order.
Values: ordinal, culture, culture_ignore_case
static_position string "keep" after: using static directives come after the plain usings, sorted (System ones first with system_first); keep: they stay where they are.
Values: keep, after
alias_position string "keep" after: alias directives come after all other usings, sorted by alias name; keep: they stay where they are.
Values: keep, after

Enabled by the presets: csharp/stylecop.

Suppress one line with sudsy-disable-next-line using-order in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
dotnet-format-editorconfig dotnet_sort_system_directives_first full
Roslynator RCS0015 partial no blank-line group handling
Roslynator RCS0037 partial no blank-line group handling
Roslynator RCS0038 partial no blank-line group handling
StyleCop SA1208 full
StyleCop SA1209 full
StyleCop SA1210 full
StyleCop SA1211 full
StyleCop SA1216 full
StyleCop SA1217 full