Elements Order - C# Coding Standard 14

In any programming language, we may use several type of elements like classes, variables, properties, methods, enums, structs,...etc. To maintain the consistency across code base and to improve the code readability, we should declare all these elements in a proper order. Today we discuss what is the good practice to declare elements in a proper format.

 

The order of the C# class file should be “using” statements followed by namespaces.

 

Any namespace should follow below order while declaring any elements.

 

1.            Delegates

2.            Enums

3.            Interfaces

4.            Structs

5.            Classes

 

C# class should follow below order to declare the elements.

 

1.            Constants

2.            Fields

3.            Constructors

4.            Finalizers

5.            Delegates

6.            Events

7.            Enums

8.            Interfaces

9.            Properties

10.          Indexers

11.          Methods

12.          Structs

 

If there are more than one elements of sane type, then they should follow the order based on their accessibility level as below.

1.            Public

2.            Internal

3.            ProtectedInternal

4.            Protected

5.            Private 

Enums, Interfaces, and Structs need not to be declared only at class level, they can also declare at namespace level also. Use #region/#endregion to group namespace and class level elements.