C# Coding Style Guide
C# Coding Style Guide
Contents
1 About the C♯ Coding Style Guide............................................................1
2 File Organization.................................................................................1
3 Indentation........................................................................................2
4 Comments..........................................................................................3
5 Declarations.......................................................................................4
6 Statements........................................................................................5
7 White Space.......................................................................................7
8 Naming Conventions............................................................................9
9 Programming Practices.......................................................................11
10 Code Examples................................................................................12
2 File Organization
2.1 C♯ Sourcefiles
Keep your classes/files short, don't exceed 2000 LOC, divide your code up, make
structures clearer. Put every class in a separate file and name the file like the class
name (with .cs as extension of course). This convention makes things much easier.
3 Indentation
3.1 Wrapping Lines
When an expression will not fit on a single line, break it up according to these general
principles:
longMethodCall(expr1, expr2,
expr3, expr4, expr5);
PREFER:
var = a * b / (c - g + f) +
4 * z;
The first is preferred, since the break occurs outside the paranthesized
expression (higher level rule). Note that you indent with tabs to the
indentation level and then with spaces to the breaking position in our
example this would be:
> var = a * b / (c - g + f) +
> ......4 * z;
Where '>' are tab chars and '.' are spaces. (the spaces after the tab char are the
indent with of the tab). A good coding practice is to make the tab and space chars
visible in the editor which is used.
4 Comments
4.1 Block Comments
Block comments should usually be avoided. For descriptions use of the /// comments
to give C♯ standard descriptions is recommended. When you wish to use block
comments you should use the following style :
/* Line 1
* Line 2
* Line 3
*/
As this will set off the block visually from code for the (human) reader. Alternatively
you might use this old fashioned C style for single line comments, even though it is
not recommended. In case you use this style, a line break should follow the
comment, as it is hard to see code proceeded by comments in the same line:
/// <summary>
/// This class...
/// </summary>
All lines must be preceded by three slashes to be accepted as XML comment lines.
Tags fall into two categories:
• Documentation items
• Formatting/Referencing
5 Declarations
5.1 Number of Declarations per Line
One declaration per line is recommended since it encourages commenting1. In other
words,
Do not put more than one variable or variables of different types on the same line
when declaring them. Example:
The above example also demonstrates the drawbacks of non-obvious variable names.
Be clear when naming variables.
1 Of course, using self-explanatory variable names such as indentLevel make these comments obsolete.
• The opening brace "{" appears in the next line after the declaration
statement.
• The closing brace "}" starts a line by itself indented to match its
corresponding opening brace.
For example:
namespace MyNamespace
{
// namespace contents
}
For example:
void Inc()
{
++myInt;
}
For example:
if (condition) {
DoSomething();
...
}
if (condition) {
DoSomething();
...
} else {
DoSomethingOther();
...
}
if (condition) {
DoSomething();
...
} else if (condition) {
DoSomethingOther();
...
} else {
DoSomethingOtherAgain();
...
}
Note: Generally use brackets even if there is only one statement in the loop.
while (condition) {
...
}
while (condition) ;
do {
...
} while (condition);
switch (condition) {
case A:
...
break;
case B:
...
break;
default:
...
break;
}
try {
...
} catch (Exception) {}
or
try {
or
try {
...
} catch (Exception e) {
...
} finally {
...
}
7 White Space
7.1 Blank Lines
Blank lines improve readability. They set off blocks of code which are in themselves
logically related.
Two blank lines should always be used between:
Methods
Properties
Local variables in a method and its first statement
Logical sections inside a method to improve readability
Note that you should always indent blank lines to the correct indent level instead of
leaving them blank or more worse using another indentation level. This insertion of
new statements in these lines much easier.
Single spaces surround operators (except unary operators like increment or logical
not), example:
Use spaces for the table like formatting and not tabs because the table formatting
may look strange in special tab intent levels.
Generally the use of underscore characters inside names and naming according to the
guidelines for Hungarian notation are considered bad practice.
Hungarian notation is a defined set of pre and postfixes which are applied to names to
reflect the type of the variable. This style of naming was widely used in early Windows
programming, but now is obsolete or at least should be considered deprecated. Using
Hungarian notation is not allowed if you follow this guide.
And remember: a good variable name describes the semantic not the type.
An exception to this rule is GUI code. All fields and variable names that contain GUI
elements like button should be postfixed with their type name without abbreviations.
For example:
System.Windows.Forms.Button cancelButton;
System.Windows.Forms.TextBox nameTextBox;
9 Programming Practices
9.1 Visibility
Do not make any instance or class variable public, make them private. Try to avoid
the “private” keyword this is the standard modifier and all C# programmers should
know that therefore just write nothing.
Use properties for class variables instead. You may use public static fields (or const)
as an exception to this rule, but be careful with it.
void DoSomething()
{
if (test == Test.TestMe) {
//...stuff gets done
} else {
//...other stuff gets done
}
}
}
}