Important: Use CSharpGuidelines for any topics not covered in this document.
The coding standard is primarily based on CSharpGuidelines (by Aviva Solutions) but differs from it in these ways:
-
Structure of the document is adapted from C# Coding Standards for .NET and C# Coding Style Guide.
-
Some examples were taken from other sources listed under References.
This will make your code more readable and also make it easier to find the .cs file for a particular class.
// .NET namespaces first
using System;
using System.Collections;
// Then any other namespaces in alphabetical order
using Company.Business;
using Company.Standard;
using Telerik.Ajax;
using Telerik.WebControls;
i. Member variable
ii. Constructors and Finalizer
iii. Nested Enums, Structs and Classes
iv. Properties
v. Methods
Note
|
Sequence declaration within type groups are based on StyleCop’s SA1202 ordering: |
// A Hello World! program in C#.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
}
When an expression does not fit, follow the general guidelines:
-
Break after a comma
-
Break after an operator
-
Align the new line with the beginning of the expression at the same level on the previous line
-
Prefer higher-level breaks to lower-level breaks
Example of breaking up method calls:
longMethodCall(expr1, expr2,
expr3, expr4, expr5);
longMethodCall(expr1, expr2
,expr3, expr4, expr5);
Example of breaking an arithmetic expression:
var result = a * b / (c - g + f) +
4 * z;
var result = a * b / (c - g +
f) + 4 * z;
The top one is preferred, since the break occurs outside the parenthesized expression, which is higher-level.
This is known as the Allman style.
while (x == y)
{
firstMethod();
secondMethod();
}
lastMethod();
-
Keywords like
if
,while
should be followed by a white space. -
Semicolons in
for
statements should be followed by a white space. -
Commas should be followed by a white space.
-
Add a white space around operators like
+
,-
,==
etc. -
Do not add white space after
(
and before)
.
Examples:
a = (b + c) * d;
while (true) {
doSomething(a, b, c, d)
for (i = 0; i < 10; i++) {
a=(b+c)*d;
while(true){
doSomething(a,b,c,d)
for(i=0;i<10;i++){
English is the preferred language for international development.
Note
|
Pascal casing: the first letter of every word is capitalized. |
Language element | Casing | Example |
---|---|---|
Class, Struct |
Pascal |
|
Interface |
Pascal |
|
Enumeration type |
Pascal |
|
Enumeration values |
Pascal |
|
Event |
Pascal |
|
Private field |
Camel |
|
Protected field |
Pascal |
|
Constant field |
Pascal |
|
Constant local variable |
Camel |
|
Read-only static field |
Pascal |
|
Local variable |
Camel |
|
Method |
Pascal |
|
Namespace |
Pascal |
|
Parameter |
Camel |
|
Type parameter |
Pascal |
|
Property |
Pascal |
|
Unless the full name is excessive:
-
Avoid abbreviations longer than 5 characters.
-
Abbreviations must be widely known and accepted.
-
Use upper case for 2-character abbreviations, and Pascal Case for longer abbreviations.
UIControl
HtmlSource
UiControl
HTMLSource
Examples: CanEvaluate
, IsVisible
, HasLicense
.
Note
|
Avoid boolean variables that represent the negation of things. e.g., use |
Hungarian notation is a defined set of pre and postfixes which are applied to names to reflect the type of the variable. This style was used in early Windows programming, but is now obsolete.
Name
Colors
strName
ColorsEnum
Note
|
Exception: All fields and variable names that contain GUI elements like button should be postfixed with their type name without abbreviations. e.g., |
-
Place the comment on a separate line, not at the end of a line of code.
-
Begin comment text with an upper case letter.
-
Insert one space between comment delimiter (
//
) and comment text. -
Use
//
or///
but never/* … */
. -
The length of comment should not exceed the length of code.
Documenting your code allows Visual Studio to pop-up the documentation when your class is used somewhere else. You can form your documentation using XML tags.
/// <summary>
/// Get a value indicating whether the user has a license.
/// </summary>
/// <returns>
/// <c>true</c> if the user has a license; otherwise <c>false</c>.
/// </returns>
public bool HasLicense() { ... }
Explicitly declare all identifiers with the appropriate access modifiers instead of allowing the default.
private void WriteEvent(string message)
void WriteEvent(string message)
short
int
long
string
Int16
Int32
Int64
String
When the type of a variable is clear from the context, use var in the declaration.
var welcomeMessage = "This is a welcome message!";
var account = new Account();
Do not use var when the type is not apparent from the right side of the assignment.
int result = ExampleClass.ResultSoFar();
Note
|
To know more about when to use/not to use implicit typing read Uses and misuses of implicit typing. |
var startInfo = new ProcessStartInfo("myapp.exe");
{
StandardOutput = Console.Output,
UseShellExecute = true
};
var startInfo = new ProcessStartInfo("myapp.exe");
startInfo.StandardOutput = Console.Output;
startInfo.UseShellExecute = true;
-
C# Coding Coventions (C# Programming Guide) — From Microsoft
-
C# Coding Standards for .NET — By Lance Hunt
-
C# Coding Style Guide — By Mike Krüger
-
CSharpGuidelines — From Aviva Solutions