Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 494
mmcdole Avatar asked Sep 29 '08 20:09

mmcdole


People also ask

When you are organizing the elements in a class which order is preferred?

Within a class, struct, or interface, elements must be positioned in the following order: Fields. Constructors. Finalizers (Destructors)

What does class contains in C#?

A class is a data structure that may contain data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, finalizers, and static constructors), and nested types.

What is constructor in C sharp?

A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created.

Which of the following statement is correct about constructor in C sharp net?

7. Which of the following statements is correct about constructors in C#.NET? Explanation: Static constructor is a constructor which can be called before any object of class is created or any static method is invoked.


2 Answers

According to the StyleCop Rules Documentation the ordering is as follows.

Within a class, struct or interface: (SA1201 and SA1203)

  • Constant Fields
  • Fields
  • Constructors
  • Finalizers (Destructors)
  • Delegates
  • Events
  • Enums
  • Interfaces (interface implementations)
  • Properties
  • Indexers
  • Methods
  • Structs
  • Classes

Within each of these groups order by access: (SA1202)

  • public
  • internal
  • protected internal
  • protected
  • private

Within each of the access groups, order by static, then non-static: (SA1204)

  • static
  • non-static

Within each of the static/non-static groups of fields, order by readonly, then non-readonly : (SA1214 and SA1215)

  • readonly
  • non-readonly

An unrolled list is 130 lines long, so I won't unroll it here. The methods part unrolled is:

  • public static methods
  • public methods
  • internal static methods
  • internal methods
  • protected internal static methods
  • protected internal methods
  • protected static methods
  • protected methods
  • private static methods
  • private methods

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.

like image 64
Jonathan Wright Avatar answered Sep 18 '22 11:09

Jonathan Wright


Rather than grouping by visibility or by type of item (field, property, method, etc.), how about grouping by functionality?

like image 23
Ryan Lundy Avatar answered Sep 22 '22 11:09

Ryan Lundy