There seem to be many different ways of organizing methods in a class. I could group methods by access, and order them alphabetically. I could group related methods together. I could use a mix of the two, or something else entirely. Is there a standard way to approach this? If not, how do you approach it?
Within a class, struct, or interface, elements must be positioned in the following order: Fields. Constructors. Finalizers (Destructors)
StyleCop enforces some things here:
Within a class, struct, or interface, elements must be positioned in the following order:
- Fields
- Constructors
- Finalizers (Destructors)
- Delegates
- Events
- Enums
- Interfaces
- Properties
- Indexers
- Methods
- Structs
- Classes
Furthermore, elements are ordered by access:
public
internal
protected internal
protected
private
As well as a few other rules:
static
elements have to appear before instance elements.This might be a good baseline to start. As for additional ordering rules, I usually group related methods together.
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