Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a standard way of laying out a C# file? As in, Fields, then Properties, then Constructors, etc?

Here's what I normally do, but I'm wondering if there's a standard way?

  1. Nested Classes or Enums
  2. Fields
  3. Properties
  4. Events
  5. Constructors
  6. Public Methods
  7. Private Methods

Do people group their fields together, or do they put them with the properties? Or do people not worry about an order? Visual Studio seems to make it so hard to do.

Edit: Moved other part about ReSharper here: Make Resharper respect your preference for code order.

like image 530
Ray Avatar asked Mar 02 '09 20:03

Ray


People also ask

Where is the best position for air conditioner?

Fact: Hot air rises up and cool air sets down. That is why the air conditioner should be placed high on the wall rather than placing it low. This way the room will get cooler fast. Also room's central location is the best position for circulating cool air in it most efficiently.

Does air conditioner placement matter?

In short – yes, it does matter! There are several things that need to be considered before choosing a location when installing an air conditioning unit. The main factors are finding a location that both maximizes the unit's efficiency and is convenient for the homeowner.

Does an air conditioner have to be perfectly level?

We recommend you do not attempt to level the outdoor unit on your own. If you try to move or level the AC unit yourself, you risk bending the coolant lines, causing a leak, and allowing refrigerant to escape into the atmosphere. Even a small kink in the copper tubing can cause catastrophe in your AC equipment.


3 Answers

I tend to use Microsoft StyleCop, which has a set order according to rule SA1201:

Cause An element within a C# code file is out of order in relation to the other elements in the code.

Rule Description A violation of this rule occurs when the code elements within a file do not follow a standard ordering scheme.

To comply with this rule, elements at the file root level or within a namespace must be positioned in the following order:

  • Extern Alias Directives
  • Using Directives
  • Namespaces
  • Delegates
  • Enums
  • Interfaces
  • Structs
  • Classes

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

Complying with a standard ordering scheme based on element type can increase the readability and maintainability of the file and encourage code reuse.

When implementing an interface, it is sometimes desirable to group all members of the interface next to one another. This will sometimes require violating this rule, if the interface contains elements of different types. This problem can be solved through the use of partial classes.

  1. Add the partial attribute to the class, if the class is not already partial.

  2. Add a second partial class with the same name. It is possible to place this in the same file, just below the original class, or within a second file.

  3. Move the interface inheritance and all members of the interface implementation to the second part of the class.

like image 166
chills42 Avatar answered Oct 13 '22 01:10

chills42


I think there's no best way. There are two important things to consider when it comes to layout. The first most important thing is consistency. Pick an approach and make sure that the entire team agrees and applies the layout. Secondly, if your class gets big enough that you are searching for where those pesky properties live (or have to implement regions to make them easier to find), then your class is probably too large. Consider sniffing it, and refactoring based on what you smell.

To answer the reshaper question, check under Type Members Layout in Options (under the C# node). It's not simple, but it is possible to change the layout order.

like image 11
Michael Meadows Avatar answered Oct 13 '22 03:10

Michael Meadows


I don't believe regions are necessarily a sign of bad code. But to determine that you will have to review what you have. As I've stated here this is how I regionize my code.


Enumerations
Declarations
Constructors
Methods
Event Handlers
Properties

But the main thing is keeping it consistent and purposeful.

like image 3
Pat Avatar answered Oct 13 '22 01:10

Pat