C# coding standards

Kia Ora. In my opinion programmers should follow some coding standards to keep the code consistent and make it more readable. In this blog I will share some general C# coding standard. After studying few standards I have developed one which best suits my requirements. By no means this is a comprehensive list, but cover most of the general aspects. It’s always a good idea to develop the habit of following a standard. I would recommend you to develop your own standard, but here is a list to give you a general idea.

  1. One Namespace per file and one class per file.
  2. Order in which members of a class can be organized:
    • Member variables.
    • Constructors & Finalizers.
    • Nested Enums, Structs, and Classes.
    • Properties
    • Methods
  3. Sequence declarations within type groups based upon access modifier and visibility:
    • Public
    • Protected
    • Internal
    • Private
  4. Naming convention
    “c” = camelCase
    “P” = PascalCase
    “_” = Prefix with _Underscore
    “I” = Prefix with “I”

    Identifiers Pattern Example
    Project file P MyFirstProject
    Namespace P MyCollection
    Class or Struct P MyClass
    Interface IP ICustomer
    Instance field _c _firstName
    Variables c temp
    Static field _c _countSize
    Static variables c lifeExpected
    Constant _c _maxAge
    Enum P Volume
    Property P FirstName
    Delegate P PerformCalculation
    Event P MouseEventHandler
    Methods P ComputeSomething
    Parameter c tempName
  5. Use appropriate prefix for each of the ui element.
    Control Prefix
    Button btn
    CheckBox chk
    ComboBox cb
    Form frm
    Label lbl
    ListBox lst
    Panel pnl
    RadioButton rdo
    RichTextBox rtxt
    TextBox txt
  6. Always use a Tab & Indention size of 4.
  7. Use // or /// but not /* … */ and do not flowerbox.
  8. Always place curly braces ({ and }) on a new line.
  9. Always use curly braces ({ and }) when optional.
  10. Add a space between keyword and parentheses ‘()’ of conditional and loop statements. Also use single space before and after operators. Example:
    if (x == 0)
    for (int i = 0; i < 5; i++)

    Note: I prefer not to add space in method definition (i.e. between method name and parentheses). But as suggested earlier it’s your choice.
  11. Avoid evaluating Boolean conditions against true or false.
    if  (isProcessOpen) {…}
    in place of
    if  (isProcessOpen == true) {…}
  12. Try to keep instance and static fields private. Use Property if you need to access it outside the class.
  13. Try to use the “@” prefix for string literals instead of escaped strings.
  14. Always use the built-in C# data type aliases, not the .NET common type system (CTS).
    short not System.Int16
    int not System.Int32
    long not System.Int64
    string not System.String
  15. While using try-catch for exception handling always catch only the specific exception, not generic exception.

I hope you will find these standards useful. If you need a more detailed list, please read the references below.


John, Tony. C# coding standards and best programming practices. http://www.dotnetspider.com/tutorials/BestPractices.aspx

Lowy, Juval (2008). C# coding standards. http://www.idesign.net/idesign/DesktopDefault.aspx

Hunt, Lance (2007). C# coding standards for .NET. http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx


My first blog

Kia Ora! Life never stops surprising me. I never though that I will ever venture into the world of blogging, but here I am writing my first blog. I will attempt to write on variety of topics with main focus on information technology and some socio-economic issues. I hope the readers will be able to feel my passion and love for programming and information technology in my blogs.

Happy reading…