Ordering
Using directives must be placed within a namespace (SA1200).
Elements at the file root level or within a namespace must be positioned in the following order (SA1201):
extern alias directives
using directives
namespaces
delegates
enumerations
interfaces
structs
classes
Within a class, struct or interface, the following positioning must be applied:
fields
constructors
finalizers (destructors)
delegates
events
enumerations
interfaces
properties
indexers
methods
structs
classes
Elements must be ordered by accessibility (SA1202). Adjacent elements of the same type must adhere to the following positioning:
public
internal
protected internal
protected
private
Constants must appear before fields (SA1203).
Static elements must appear before instance elements (SA1204).
Partial elements must declare access (SA1205).
Declaration keywords must adhere to the following ordering scheme (SA1206):
access modifiers
static
all other keywords
The keyword protected must be positioned before the keyword internal when declaring a protected internal C# element (SA1207).
Property accessors must adhere to the following ordering scheme (SA1212):
get accessors
set accessors
Event accessors must adhere to the following ordering scheme (SA123):
add accessor
remove accessor
Static read-only elements must appear before static non-read-only elements (SA1214).
Instance read-only elements must appear before instance non-read-only elements (SA1215).
System using directives must be placed before other using directives (SA1208).
Using alias directives must be placed after other using directives (SA1209).
Using directives must be ordered alphabetically by namespace (SA1210).
Using alias directives must be ordered alphabetically by alias name (SA1211).
Note
- In Visual Studio, you can easily sort the using directives by positioning the cursor on a using directive in the code editor and selecting Organize Usings > Sort Usings from the context menu. In case you want to also remove unnecessary using directives, select Organize Usings > Remove and Sort Usings from the context menu.
- In order to let Visual Studio put System directives first when sorting namespaces, go to Tools > Options. In the list on the left, go to Text Editor > C# > Advanced and select the checkbox Place 'System' directives first when sorting usings.
- If "#define" preprocessor directives are used, make sure they are present and not commented out when using the above. Failing to do so can cause quick action build failures.