c# Coding StyleGuide
c# Coding StyleGuide
Contents
1 About the C♯ Coding Style Guide1
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.
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.
2
Don't use spaces for indentation - use tabs!
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:
Block comments may be useful in rare cases, refer to the TechNote 'The fine Art of
Commenting' for an example. Generally block comments are useful for comment out
large sections of code.
/// <summary>
/// This class...
/// </summary>
3
/// This exception gets thrown as soon as a
/// Bogus flag gets set.
/// </exception>
All lines must be preceded by three slashes to be accepted as XML comment lines.
Tags fall into two categories:
Documentation items
Formatting/Referencing
The first category contains tags like <summary>, <param> or <exception>. These
represent items that represent the elements of a program's API which must be documented for
the program to be useful to other programmers. These tags usually have attributes such as
name or cref as demonstrated in the multi line example above. These attributes are
checked by the compiler, so they should be valid.
The latter category governs the layout of the documentation, using tags such as <code>,
<list> or <para>.
Documentation can then be generated using the 'documentation' item in the ♯develop 'build'
menu. The documentation generated is in HTMLHelp format.
For a fuller explanation of XML comments see the Microsoft .net framework SDK
documentation. For information on commenting best practice and further issues related to
commenting, see the TechNote 'The fine Art of Commenting'.
5 Declarations
5.1 Number of Declarations per Line
One declaration per line is recommended since it encourages commenting. 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.
5.2 Initialization
Try to initialize local variables as soon as they are declared. For example:
4
5.3 Class, Interface and Namespace Declarations
When coding C♯ classes, interfaces and namespaces , the following formatting rules should
be followed:
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()
{
5
++myInt;
}
For example:
if (condition) {
DoSomething();
...
}
if (condition) {
DoSomething();
...
} else {
DoSomethingOther();
6
...
}
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) {
7
case A:
...
break;
case B:
...
break;
default:
...
break;
}
try {
...
} catch (Exception) {}
or
try {
...
} catch (Exception e) {
...
}
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
8
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.
9
8 Naming Conventions
8.1 Capitalization Styles
8.1.1 Pascal Casing
This convention capitalizes the first character of each word (as in TestCounter).
System.Windows.Forms.Button cancelButton;
System.Windows.Forms.TextBox nameTextBox;
10
8.2.3 Enum Naming Guidelines
Use Pascal Casing for enum value names and enum type names
Don’t prefix (or suffix) a enum type or enum values
Use singular names for enums
Use plural name for bit fields.
11
public Fields Pascal Casing
Methods Pascal Casing
Namespace Pascal Casing
Property Pascal Casing
Protected/private Fields Camel Casing
Parameters Camel Casing
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.
12
10 Code Examples
10.1 Brace placement example
namespace ShowMeTheBracket
{
public enum Test {
TestMe,
TestYou
}
void DoSomething()
{
if (test == Test.TestMe) {
//...stuff gets done
} else {
//...other stuff gets done
}
}
}
}
13
10.2 Variable naming example
instead of :
14