Is there an official C# guideline for the order of items in terms of class structure?
Does it go:
- Public Fields
- Private Fields
- Properties
- Constructors
- Methods
?
I'm curious if there is a hard and fast rule about the order of items? I'm kind of all over the place. I want to stick with a particular standard so I can do it everywhere.
The real problem is my more complex properties end up looking a lot like methods and they feel out of place at the top before the constructor.
Any tips/suggestions?
Usually I try to follow the next pattern:
Each part (static and instance) consists of the following member types:
Then the members are sorted by visibility (from less to more visible):
The order is not a dogma: simple classes are easier to read, however, more complex classes need context-specific grouping.
According to the StyleCop Rules Documentation the ordering is as follows.
Within a class, struct or interface: (SA1201 and SA1203)
Within each of these groups order by access: (SA1202)
Within each of the access groups, order by static, then non-static: (SA1204)
Within each of the static/non-static groups of fields, order by readonly, then non-readonly : (SA1214 and SA1215)
An unrolled list is 130 lines long, so I won't unroll it here. The methods part unrolled is:
The documentation notes that if the prescribed order isn't suitable - say, multiple interfaces are being implemented, and the interface methods and properties should be grouped together - then use a partial class to group the related methods and properties together.
The closest you're likely to find is "Design Guidelines, Managed code and the .NET Framework" (http://blogs.msdn.com/brada/articles/361363.aspx) by Brad Abrams
Many standards are outlined here. The relevant section is 2.8 I think.