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.
- One Namespace per file and one class per file.
- Order in which members of a class can be organized:
- Member variables.
- Constructors & Finalizers.
- Nested Enums, Structs, and Classes.
- Properties
- Methods
- Sequence declarations within type groups based upon access modifier and visibility:
- Public
- Protected
- Internal
- Private
- 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 - 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 - Always use a Tab & Indention size of 4.
- Use // or /// but not /* … */ and do not flowerbox.
- Always place curly braces ({ and }) on a new line.
- Always use curly braces ({ and }) when optional.
- 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. - Avoid evaluating Boolean conditions against true or false.
Example:
Use
if (isProcessOpen) {…}
in place of
if (isProcessOpen == true) {…} - Try to keep instance and static fields private. Use Property if you need to access it outside the class.
- Try to use the “@” prefix for string literals instead of escaped strings.
- Always use the built-in C# data type aliases, not the .NET common type system (CTS).
Example:
short not System.Int16
int not System.Int32
long not System.Int64
string not System.String - 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.
References
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