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:
MyClass.cs → public class MyClass { ... }
System.Windows.Forms.Control
should use the path System\Windows\Forms\Control.cs
Do not use namespace name with dots. This will make it easier to map namespaces to the directory layout.
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;
Sequence declaration within type groups are based on StyleCop's SA1202 ordering: public, internal, protected internal, protected, private
.
// 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:
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 https://en.wikipedia.org/wiki/Indent_style#Allman_style[Allman style].
while (x < y)
{
firstMethod();
secondMethod();
}
lastMethod();
Such as having only one statement in the if
clause. This is to enforce consistency.
if (x > y)
{
doSomething();
}
if
, while
should be followed by a white space.for
statements should be followed by a white space.+
, -
, ==
etc.(
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.
Language element | Casing | Example |
---|---|---|
Class, Struct | Pascal | AppDomain |
Interface | Pascal | IBusinessService |
Enumeration type | Pascal | ErrorLevel |
Enumeration values | Pascal | FatalError |
Event | Pascal | Click |
Private field | Camel | listItem |
Protected field | Pascal | MainPanel |
Constant field | Pascal | MaximumItems |
Constant local variable | Camel | maximumItems |
Read-only static field | Pascal | RedValue |
Local variable | Camel | listOfValues |
Method | Pascal | ToString |
Namespace | Pascal | System.Drawing |
Parameter | Camel | typeName |
Type parameter | Pascal | TView |
Property | Pascal | BackColor |
Unless the full name is excessive:
UIControl
HtmlSource
UiControl
HTMLSource
Can
, Is
, or Has
.Examples: CanEvaluate
, IsVisible
, HasLicense
.
Avoid boolean variables that represent the negation of things. e.g., use IsInitialized
instead of IsNotInitialized
.
Customer.Name
Customer.CustomerName
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
Exception: All fields and variable names that contain GUI elements like button should be postfixed with their type name without abbreviations. e.g., cancelButton
, nameTextBox
.
//
) and comment text.//
or ///
but never /* ... */
.Documenting your code allows Visual Studio to pop-up the documentation when your class is used somewhere else. You can form your documentation using https://msdn.microsoft.com/en-us/library/5ast78ax.aspx[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();
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;