4

When creating a new C# class I am not certain of what the best logical order for declaring properties, event delegates, functions, function overrides, etc. are and what considerations should be taken into account when deciding on that order.

Typically when creating the code behind class of a WebUserControl I end up placing things in this order:

  1. Events
  2. Properties
  3. Life Cycle Event override functions
  4. Other functions

Is there a more logical way to do this and what considerations should I be making when deciding on how to order these elements of the class within the class file?

durron597
  • 31,968
  • 17
  • 99
  • 158
Chris Magnuson
  • 5,780
  • 7
  • 34
  • 37
  • 2
    Once you figured that out, maybe yo want to take a look at this free tool: http://visualstudiogallery.msdn.microsoft.com/en-us/800978aa-2aac-4440-8bdf-6d1a76a5c23c – Marcel Jackwerth Oct 21 '09 at 12:53
  • Thank you. Regionerate seems like it would be very useful. Currently I have all outlining disabled as the code base that I am working with is really messed up and I need to look at everything in each class to confirm it's behavior but for re factored code this could be very useful. – Chris Magnuson Oct 21 '09 at 13:44

8 Answers8

7

Does not make any difference to the compilation, you might want to wrap the sections in #region to make it easier for someone reading your code to know where they are and keeping them organized. It probably should be a coding standard for your company so all code is organized similarly and is less frustrating to look at...

CSharpAtl
  • 7,374
  • 8
  • 39
  • 53
  • Agreed, the only "point" to having any sort of logical organization is for your own (and other developers) benefit to be able to find things later when you need to go back. Use whatever feels good and looks right to you. – BBlake Oct 21 '09 at 12:48
  • 2
    #region should be used sparingly; they hide code and often add little or no value; http://www.codinghorror.com/blog/archives/001147.html – g . Oct 21 '09 at 13:38
4

This is a style preference. A lot of people do:

  1. Properties
  2. Overrides
  3. Other functions
  4. Events

Some people also separate each using #regions.

Espo
  • 41,399
  • 21
  • 132
  • 159
BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
4

Whatever StyleCop tells me to. :)

Chris
  • 4,030
  • 4
  • 47
  • 76
3

Just go with whatever makes sense to you, or is in your coding standards. As long as it's consistent, it doesn't really matter...

Paddy
  • 33,309
  • 15
  • 79
  • 114
3

I don't think the order is important as long as you are consistent with your style. Consider the use of regions, but don't go overboard with them. As a general rule of thumb for me, I avoid all nested regions. That is just too much code hiding.

Bob
  • 97,670
  • 29
  • 122
  • 130
3

Consistency is more important as a certain order.

Personally, I like to find members quickly by visibility:

  • public
  • protected
  • private

As a user of the class you usually just need the public interface, if you derive, you need also the protected interface, only if you are changing the class itself you need to look at the private stuff.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
3

As long as you have a standard style throughout your development team, it is whatever works for you. If you are using Visual Studio, then with the class viewer and member drop down menu, it becomes even more irrelevant. Check out the Regionerate addin - it gives you the options to sort members by type or alphabetically and also add regions by type, visibility etc. If not of the default settings suit you, you can define your own.

Philip Wallace
  • 7,905
  • 3
  • 28
  • 40
2

I usually put member declarations at the top, then methods (life-cycle and others), then event handlers, and finally properties. For methods, I try to order them roughly in the order they will be called. For example. the methods called when loading a page come first, then the ones called when saving a page submission. Properties and event handlers are at the end because they are generally trivial, so I put them out of the way.

Ray
  • 21,485
  • 5
  • 48
  • 64