C# Coding Standards and Best Programming Practice Part-3

March 26, 2013 // In: C#

coding standards and programming practice

16. The curly braces should be on a separate line and not in the same line as if, for, etc.

17. Use a single space before and after each operator and brackets.

18. Prefix boolean variables, properties and methods with “is” or similar prefixes.

19. Use appropriate prefix for the UI elements so that you can identify them from the rest of the variables

Control Prefix Control Prefix
Label lbl Repeater rep
TextBox txt Checkbox chk
DataGrid dtg CheckBoxList cbl
Button btn RadioButton rdo
ImageButton imb RadioButtonList rbl
Hyperlink hlk Image img
DropDownList ddl Panel pnl
ListBox lst PlaceHolder phd
DataList dtl Table tbl
Validators val







20. Use TAB for indentation. Do not use SPACES.  Define the Tab size as 4

21. Comments should be in the same level as the code (use the same level of indentation)

22. Curly braces ( {} ) should be in the same level as the code outside the braces.  braces should be at same level

23. Use one blank line to separate logical groups of code.


24.  The curly braces should be on a separate line and not in the same line as if, for etc.

25.  Use a single space before and after each operator and brackets.

26.  Use #region to group related pieces of code together. If you use proper grouping using #region, the page should like this when all definitions are collapsed

use of region in dot net


27. Do not make the member variables public or protected. Keep them private and expose public/protected Properties

28.  Do not programmatically click a button to execute the same action you have written in the button click event. Rather, call the same method which is called by the button click event handler.

29.  Never hardcode a path or drive name in code. Get the application path programmatically and use relative path

30.  Error messages should help the user to solve the problem. Never give error messages like “Error in Application”, “There is an error” etc. Instead give specific messages like “Failed to update database. Please make sure the login id and password are correct.”

31.  Avoid public methods and properties, unless they really need to be accessed from outside the class. Use “internal” if they are accessed only within the same assembly.

32.  Avoid passing too many parameters to a method. If you have more than 4~5 parameters, it is a good candidate to define a class or structure

33.  If you are opening database connections, sockets, file stream etc, always close them in the finally block. This will ensure that even if an exception occurs after opening the connection, it will be safely closed in the finally block

34.  Use StringBuilder class instead of String when you have to manipulate string objects in a loop. The String object works in weird way in .NET. Each time you append a string, it is actually discarding the old string object and recreating a new object, which is a relatively expensive operations.

Consider the following example:


In the above example, it may look like we are just appending to the string object ‘message’. But what is happening in reality is, the string object is discarded in each iteration and recreated and appending the line to it.

35. If your loop has several iterations, then it is a good idea to use StringBuilder class instead of String object.

See the example where the String object is replaced with StringBuilder.

These are the basic standards that you must follow while developing in c#. Its not neccessary to follow same rules. You can develop your own for your team. But you must follow some rules when you are developing in a team.

Populate TreeView from Database using C#
C# Coding Standards and Best Programming Practice Part-2