Csharp Coding Guidelines

Introduction
This guide is by no means a bible on how to write good code, but shows how to format code to make readability easier, and code cleaner.

Style Guidelines
Below are good practices for code styling

Bracing
Open braces should always be at the beginning of the line after the statement that begins the block. Contents of the brace should be indented by 1 tab or 4 spaces. For example:

“case” statements should be indented from the switch statement like this:

Braces should never be considered optional. Even for single statement blocks, you should always use braces. This increases code readability and maintainability.

Single line statements
Single line statements can have braces that begin and end on the same line.

It is suggested that all control structures (if, while, for, etc.) use braces, but it is not required.

Commenting
Comments should be used to describe intention, algorithmic overview, and/or logical flow. It would be ideal, if from reading the comments alone, someone other than the author could understand a function’s intended behavior and general operation. While there are no minimum comment requirements and certainly some very small routines need no commenting at all, it is hoped that most routines will have comments reflecting the programmer’s intent and approach.

Comment Style
The // (two slashes) style of comment tags should be used in most situations. Where ever possible, place comments above the code instead of beside it. Here are some examples:

Comments can be placed at the end of a line when space allows:

Spacing
Spaces improve readability by decreasing code density. Here are some guidelines for the use of space characters within code:

Do use a single space after a comma between function arguments. Right: Wrong: Do not use a space after the parenthesis and function arguments Right: Wrong: Do not use spaces between a function name and parenthesis. Right: Wrong: Do not use spaces inside brackets. Right: Wrong: Do use a single space before flow control statements Right: Wrong: Do use a single space before and after comparison operators Right: Wrong:

Naming

 * Do not use Hungarian notation
 * Do not use a prefix for member variables (_, m_, s_, etc.). If you want to distinguish between local and member variables you should use “this.” in C# and “Me.” in VB.NET.
 * Do use camelCasing for member variables
 * Do use camelCasing for parameters
 * Do use camelCasing for local variables
 * Do use PascalCasing for function, property, event, and class names
 * Do prefix interfaces names with “I”
 * Do not prefix enums, classes, or delegates with any letter

File Organization

 * Source files should contain only one public type, although multiple internal classes are allowed
 * Source files should be given the name of the public class in the file
 * Classes member should be alphabetized, and grouped into sections (Fields, Constructors, Properties, Events, Methods, Private interface implementations, Nested types)

Example