Skip to content

member-order

C#styleno fixdefault: offcsharp/stylecop

Reports a member that is out of order compared with the member before it, in the order of the element_order option: the kind (fields, constructors, finalizers, delegates, events, enums, interfaces, properties, indexers, operators, methods, structs, classes; in a namespace or file: namespaces, delegates, enums, interfaces, structs, classes), the access (public, internal, protected internal, protected, private protected, private), constants before other fields, static before instance members, readonly fields before the others. A trait counts only between members that agree on the traits listed before it. No fix: moving members would have to take their comments and directives along.

A fixed order lets a reader find a member without reading the class: fields at the top, constructors next, then the rest by visibility. Different files of a project then look the same.

To fix, move the member to its place.

Reported
class A {
void M() { }
private int b;
public int a;
const int C = 1;
static int s;
int i;
readonly int r;
static readonly int sr;
public A() { }
}
class B { }
enum E { }
csharp:
lint:
rules:
member-order:
level: warn # off (default), warn, error
options:
element_order: ["kind","accessibility","constant","static","readonly"]
Option Type Default Description
element_order list ["kind","accessibility","constant","static","readonly"] The traits that decide the order, most important first. A trait that is left out is not checked.
Values: kind, accessibility, constant, static, readonly

Enabled by the presets: csharp/stylecop.

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

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
StyleCop SA1201 full
StyleCop SA1202 full
StyleCop SA1203 full
StyleCop SA1204 full
StyleCop SA1214 full