0

Is there an official (ie, FX Cop, Guidance on MSDN, etc) way to organize the Properties, Methods and Events in a class file?

I know this can be seen as a "preference" and I have seen it done many different ways, typically:

  1. Constructor

  2. Properties

  3. Events

  4. Methods, in alphabetically order.

My goal is to have a well organized class file which becomes important as the size of the class file gets rather large. (Therein is a different topic, splitting a class which is too large.) It also becomes important when multiple developers are working together so there is an agreed upon standard.

So I am looking for something "official" I can use as a reference for this.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Greg Gum
  • 33,478
  • 39
  • 162
  • 233
  • 1
    The #region directive is the best tool to organize a complex class – Steve Aug 14 '13 at 12:51
  • This question is too broad for this site. You might want to try the stack exchange programmers http://programmers.stackexchange.com/ – Luis Filipe Aug 14 '13 at 12:51
  • You might be interested to take a look at this: http://stackoverflow.com/questions/1642624/microsoft-coding-standard-document – Zdeslav Vojkovic Aug 14 '13 at 12:52
  • 1
    See the accepted answer in: http://stackoverflow.com/questions/603758/whats-the-best-way-to-layout-a-c-sharp-class – sara Aug 14 '13 at 12:55
  • 1
    Have a look at the book "Clean Code" by Robert C Martin. – Matthew Watson Aug 14 '13 at 12:55
  • Don't make classes so large that you need to worry about finding a method in it. Functionality can always be broken out to support classes when necessary. The only ordering I use is at the header: fields before constructors before the rest of the class. After that: methods and properties that are related should be close together. – Anders Forsgren Aug 14 '13 at 12:56
  • @sara, yes that is a good answer and this question can be marked as duplicate. – Greg Gum Aug 14 '13 at 12:58
  • Normally I tend to place stuff in logical order, so the class can be read like an article. However, if you have trouble finding stuff in your classes, it's not because they are ordered badly. It's because your classes are _way_ too big, treat that problem instead. – Pierre-Luc Pineault Aug 14 '13 at 13:06

2 Answers2

4

I believe Stylecop used to be Microsoft's baby, might be worth taking a look at it to see what it recommends?

Stylecop

David MacCrimmon
  • 966
  • 1
  • 6
  • 19
2

You can refer to:

What's the best way to layout a C# class?

and to:

Order of items in classes: Fields, Properties, Constructors, Methods

For a detailed excellent answers.

Community
  • 1
  • 1
sara
  • 3,824
  • 9
  • 43
  • 71
  • Link only answers are not allowed. Edit the answer to include the relevant parts from the linked pages. – jgauffin Aug 14 '13 at 13:18