Use Pascal casing for type and method names and constants:
public class SomeClass
{
const int DefaultSize = 100;
public SomeMethod( )
{}
}
Use camel casing for local variable names and method
Arguments:
int number;
void MyMethod(int someNumber)
{}
Prefix interface namesI:
interface IMyInterface
{..}
Prefix private member variablesM _.
Suffix custom attribute classes
Attribute.
Suffix custom exception classes
Exception.
Name methods using verb/object pairs, suchShowDialog (
).
Methods with return values shoshould have names describing
Values returned, suchGetObjectState ().
Use descriptive variable names.
Avoid single-character variable names, suchIOr
T. UseIndexOrTempInstead.
Avoid using Hungarian notation for public or protected
Members.
Avoid abbreviating words (suchNumInstead
Number).
Always use C # predefined types, rather than the aliases in
SystemNamespace. For example:
object NOT Object
string NOT String
intNOT Int32
With generics, use capital letters for types. Reserve suffixing
TypeFor when dealing with the. NET typeType:
//Correct:
public class LinkedList<K,T>
{...}
//Avoid:
public class LinkedList<KeyType,DataType>
{...}
Use meaningful namespace names, such as the product name or
Company name.
Avoid fully qualified type names. UseUsing
Statement instead.
Avoid puttingUsingStatement inside
Namespace.
Group all framework namespaces together and put custom or
Third-party namespaces underneath:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MyCompany;
using MyControls;
Use delegate inference instead of explicit delegate
Instantiation:
delegate void SomeDelegate( );
public void SomeMethod( )
{...}
SomeDelegate someDelegate = SomeMethod;
Maintain strict indentation. Do not use tabs or nonstandard
Indentation, such as one space. Recommended values are three or four
Spaces.
Indent comments at the same level of indentation as the code
That you are documenting.
All comments shocould pass spellchecking. Misspelled comments
Indicate sloppy development.
All member variables shocould be declared at the top, with one
Line separating them from the properties or methods:
public class MyClass
{
int m_Number;
string m_Name;
public void SomeMethod1( )
{}
public void SomeMethod2( )
{}
}
Declare a local variable as close as possible to its first
Use.
A filename shocould reflect the class it contains.
When using partial types and allocating a part per file, name
Each file after the logical part that part plays. For example:
//In MyClass.cs
public partial class MyClass
{...}
//In MyClass.Designer.cs
public partial class MyClass
{...}
Always place an open curly brace ({) In a new
Line.
With anonymous methods, mimic the code layout of a regular
Method, aligned with the anonymous delegate declaration (this complies
Placing an open curly brace in a new line ):
delegate void SomeDelegate(string someString);
//Correct:
public void InvokeMethod( )
{
SomeDelegate someDelegate = delegate(string name)
{
MessageBox.Show(name);
};
someDelegate("Juval");
}
//Avoid
public void InvokeMethod( )
{
SomeDelegate someDelegate = delegate(string name){MessageBox.Show(name);};
someDelegate("Juval");
}
Use empty parentheses on parameter-less anonymous methods. Omit
The parentheses only if the anonymous method cocould have been used on any
Delegate:
delegate void SomeDelegate( );
//Correct
SomeDelegate someDelegate1 = delegate( )
{
MessageBox.Show("Hello");
};
//Avoid
SomeDelegate someDelegate1 = delegate
{
MessageBox.Show("Hello");
};