Skip to content

accessor-order

C#stylefix: safedefault: offcsharp/stylecop

Reports a property or event whose accessors are in the wrong order: get goes before set or init, and add before remove. The fix swaps the two accessors; it is not offered when a comment stands between them or inside them.

Readers expect to meet the reading accessor first and the writing one second. One order in all properties makes a file quick to scan and makes the odd one out stand out.

Reported
class A {
int P { set { } get { return 1; } }
event System.Action E { remove { } add { } }
int Q { set { } /* why */ get { return 1; } }
}
After the fix
class A {
int P { get { return 1; } set { } }
event System.Action E { add { } remove { } }
int Q { set { } /* why */ get { return 1; } }
}
csharp:
lint:
rules:
accessor-order: warn # off (default), warn, error

Enabled by the presets: csharp/stylecop.

Suppress one line with sudsy-disable-next-line accessor-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 SA1212 full
StyleCop SA1213 full