Naming Conventions and Styles
-
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 names with I:
interface IMyInterface
{..} -
Prefix private member variables with m_.
-
Suffix custom attribute classes with Attribute.
-
Suffix custom exception classes with Exception.
-
Name methods using verb/object pairs, such as ShowDialog( ).
-
Methods with return values should have names describing the values returned, such as GetObjectState( ).
-
Use descriptive variable names.
-
Avoid single-character variable names, such as i or t. Use index or temp instead.
-
Avoid using Hungarian notation for public or protected members.
-
Avoid abbreviating words (such as num instead of number).
-
-
Always use C# predefined types, rather than the aliases in the System namespace. For example:
object NOT Object
string NOT String
intNOT Int32 -
With generics, use capital letters for types. Reserve suffixing Type for when dealing with the .NET type Type:
//Correct:
public class LinkedList<K,T>
{...}
//Avoid:
public class LinkedList<KeyType,DataType>
{...} -
Use meaningful namespace names, such as the product name or the company name.
-
Avoid fully qualified type names. Use the using statement instead.
-
Avoid putting a using statement inside a 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 should pass spellchecking. Misspelled comments indicate sloppy development.
-
All member variables should 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 should 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 with 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 could have been used on any delegate:
delegate void SomeDelegate( );
//Correct
SomeDelegate someDelegate1 = delegate( )
{
MessageBox.Show("Hello");
};
//Avoid
SomeDelegate someDelegate1 = delegate
{
MessageBox.Show("Hello");
};