This document describes rules and recommendations for developing applications and class libraries using the C# Language. The goal is to define guidelines to enforce consistent style and formatting and help developers avoid common pitfalls and mistakes.
Specifically, this document coversNaming Conventions,Coding Style,Language Usage, andObject Model Design.
This document only applies to the C# Language and the .NET Framework Common Type System(CTS) it implements. Although the C# language is implemented alongside the .NET Framework, this document does not address usage of
.NET Framework class libraries. However, common patterns and problems related to C#'s usage of the .NET Framework are addressed in a limited fashion.
Even though standards for curly-braces ({ or }) and white space(tabs vs. spaces) are always controversial, these topics are addressed here to ensure greater consistency and maintainability of source code.
Much like the ensuing coding standards, this document requires standards in order to ensure clarity when stating the rules and guidelines. Certain conventions are used throughout this document to add emphasis.
Below are some of the common conventions used throughout this document.
Coloring & Emphasis:
Blue Text colored blue indicates a C# keyword or .NET type.
Bold Text with additional emphasis to make it stand-out.
Keywords:
Always Emphasizes this rule must be enforced.
Never Emphasizes this action must not happen.
Do Not Emphasizes this action must not happen.
Avoid Emphasizes that the action should be prevented, but some exceptions may exist.
Try Emphasizes that the rule should be attempted whenever possible and appropriate.
Example Precedes text used to illustrate a rule or recommendation.
Reason Explains the thoughts and purpose behind a rule or recommendation.
The following terminology is referenced throughout this document:
Access Modifier
C# keywords public, protected, internal, and private declare the allowed code-accessibility of types and their members. Although default access modifiers vary, classes and most other members use the default of private. Notable exceptions are interfaces and enums which both default to public.
Camel Case
A word with the first letter lowercase, and the first letter of each subsequent word-part capitalized.
Example: customerName
Common Type System
The .NET Framework common type system (CTS) defines how types are declared, used, and managed. All native C# types are based upon the CTS to ensure support for cross-language integration.
Identifier
A developer defined token used to uniquely name a declared object or object instance.
Example: public class MyClassNameIdentifier { … }
Magic Number
Any numeric literal used within an expression (or to initialize a variable) that does not have an obvious or well- known meaning. This usually excludes the integers 0 or 1 and any other numeric equivalent precision that evaluates as zero.
Pascal Case
A word with the first letter capitalized, and the first letter of each subsequent word-part capitalized.
Example: CustomerName
Premature Generalization
As it applies to object model design; this is the act of creating abstractions within an object model not based upon concrete requirements or a known future need for the abstraction. In simplest terms: "Abstraction for the sake of Abstraction."
The following flags are used to help clarify or categorize certain statements:
[C#v2+]
A flag to identify rules and statements that apply only to C# Language Specification v2.0 or greater.
Quick Summary
This section contains tables describing a high-level summary of the major standards covered in this document. These tables are not comprehensive, but give a quick glance at commonly referenced elements.
"c" |
= |
camelCase |
"P" |
= |
PascalCase |
"_" |
= |
Prefix with _Underscore |
"x" |
= |
Not Applicable. |
Identifier |
Public |
Protected |
Internal |
Private |
Notes |
Project File |
P |
x |
x |
x |
Match Assembly & Namespace. |
Source File |
P |
x |
x |
x |
Match contained class. |
Other Files |
P |
x |
x |
x |
Apply where possible. |
Namespace |
P |
x |
x |
x |
Partial Project/Assembly match. |
Class or Struct |
P |
P |
P |
P |
Add suffix of subclass. |
Interface |
P |
P |
P |
P |
Prefix with a capital I. |
Generic Class [C#v2+] |
P |
P |
P |
P |
Use T or K as Type identifier. |
Method |
P |
c |
P |
c |
Use a Verb or Verb-Object pair. |
Property |
P |
P |
P |
P |
Do not prefix with Get or Set. |
Field |
P |
p |
P |
_c |
Only use Private fields. No Hungarian Notation! |
Constant |
P |
P |
P |
_c |
|
Static Field |
P |
P |
P |
_c |
Only use Private fields. |
Enum |
P |
P |
P |
P |
Options are also PascalCase. |
Delegate |
P |
P |
P |
P |
|
Event |
P |
P |
P |
P |
|
Inline Variable |
x |
x |
x |
c |
Avoid single-character and enumerated names. |
Parameter |
x |
x |
x |
c |
|
Code |
Style |
Source Files |
One Namespace per file and one class per file. |
Curly Braces |
On new line. Always use braces when optional. |
Indention |
Use tabs with size of 4. |
Comments |
Use // or /// but not /* … */ and do not flowerbox. |
Variables |
One variable per declaration. |
Code |
Style |
Native Data Types |
Use built-in C# native data types vs .NET CTS types. (Use int NOT Int32) |
Enums |
Avoid changing default type. |
Generics[C#v2+] |
Prefer Generic Types over standard or strong-typed classes. |
Properties |
Never prefix with Get or Set. |
Methods |
Use a maximum of 7 parameters. |
baseandthis |
Use only in constructors or within an override. |
Ternary conditions |
Avoid complex conditions. |
foreachstatements |
Do not modify enumerated items within a foreach statement. |
Conditionals |
Avoid evaluating Boolean conditions against true or false. No embedded assignment. Avoid embedded method invocation. |
Exceptions |
Do not use exceptions for flow control. Use throw; not throw e; when re-throwing. Only catch what you can handle. Use validation to avoid exceptions. Derive from Execption not ApplicationException. |
Events |
Always check for null before invoking. |
Locking |
Use lock() not Monitor.Enter(). Do not lock on an object type or "this". Do lock on private objects. |
Dispose()&Close() |
Always invoke them if offered, declare where needed. |
Finalizers |
Avoid. Use the C# Destructors. Do not create Finalize() method. |
AssemblyVersion |
Increment manually. |
ComVisibleAttribute |
Set to false for all assemblies. |
Consistency is the key to maintainable code. This statement is most true for naming your projects, source files, and identifiers including Fields, Variables, Properties, Methods, Parameters, Classes, Interfaces, and Namespaces.
Example: strName or iCount
Example:
// Bad!
public enum ColorsEnum {…} public class CVehicle {…}
public struct RectangleStruct {…}
Example:Customer.Name NOT Customer.CustomerName
LanceHunt.StringUtilities
Identifier |
NamingConvention |
ProjectFile |
Pascal Case. Always match Assembly Name & Root Namespace.
Example: LanceHunt.Web.csproj -> LanceHunt.Web.dll -> namespace LanceHunt.Web |
Source File |
Pascal Case. Always match Class name and file name.
Avoid including more than one Class, Enum (global), or Delegate (global) per file. Use a descriptive file name when containing multiple Class, Enum, or Delegates.
Example: MyClass.cs => public class MyClass {…} |
Resource or EmbeddedFile |
Try to use Pascal Case.
Use a name describing the file contents. |
Namespace |
Pascal Case. Try to partially match Project/Assembly Name.
Example: namespace LanceHunt.Web {…} |
Classor Struct |
Pascal Case. |
Use a noun or noun phrase for class name. |
|
Add an appropriate class-suffix when sub-classing another type when possible. |
|
Examples: |
|
private class MyClass |
|
{…} |
|
internal class SpecializedAttribute : Attribute |
|
{…} |
|
public class CustomerCollection : CollectionBase |
|
{…} |
|
public class CustomEventArgs : EventArgs |
|
{…} |
|
private struct ApplicationSettings |
|
{…} |
|
Interface |
Pascal Case. Always prefix interface name with capital "I".
Example: interface ICustomer {…} |
GenericClass
&
GenericParameter Type
[C#v2+] |
Always use a single capital letter, such as T or K.
Example: public class FifoStack<T> { public void Push(<T> obj) {…}
public <T> Pop() {…} } |
Method |
Pascal Case. Try to use aVerborVerb-Objectpair.
Example: public void Execute() {…} private string GetAssemblyVersion(Assembly target) {…} |
Property |
Pascal Case. Property name should represent the entity it returns. Never prefix property names with "Get" or "Set".
Example: public string Name { get{…} set{…} } |
Field
(Public, Protected, or Internal) |
Pascal Case. Avoid using non-private Fields! Use Properties instead.
Example: public string Name; protected IList InnerList; |
Field(Private) |
Camel Case and prefix with a single underscore (_) character.
Example: private string _name; |
Constantor StaticField |
Treat like a Field. Choose appropriate Field access-modifier above. |
Enum |
Pascal Case (both the Type and the Options). Add the FlagsAttribute to bit-mask multiple options.
Example: public enum CustomerTypes { Consumer, Commercial } |
DelegateorEvent |
Treat as a Field. Choose appropriate Field access-modifier above.
Example: public event EventHandler LoadPlugin; |
Variable(inline) |
Camel Case. Avoid using single characters like "x" or "y" except in FOR loops. Avoid enumerating variable names like text1, text2, text3 etc. |
Parameter |
Camel Case.
Example: public void Execute(string commandText, int iterations) {…} |
Coding style causes the most inconsistency and controversy between developers. Each developer has a preference, and rarely are two the same. However, consistent layout, format, and organization are key to creating maintainable code.
The following sections describe the preferred way to implement C# source code in order to create readable, clear, and consistent code that is easy to understand and maintain.
Example:
// Bad!
[Attrbute1, Attrbute2, Attrbute3] public class MyClass
{…}
// Good!
[Attrbute1, RelatedAttribute2] [Attrbute3]
[Attrbute4]
public class MyClass
{…}
Example:
// ***************************************
// Comment block
// ***************************************
Example:
// TODO: Place Database Code Here
// UNDONE: Removed P\Invoke Call due to errors
// HACK: Temporary fix until able to refactor
Example:
/// <example>
/// Add the following key to the "appSettings" section of your config:
/// <code><![CDATA[
/// <configuration>
/// <appSettings>
/// <add key="mySetting" value="myValue"/>
/// </appSettings>
/// </configuration>
/// ]]></code>
/// </example>
Example:
// Bad!
Void WriteEvent(string message)
{…}
// Good!
private Void WriteEvent(string message)
{…}
Example:
[assembly: ComVisible(false)] [ComVisible(true)]
public MyClass
{…}
Example:
short NOT System.Int16 int NOT System.Int32 long NOT System.Int64 string NOT System.String
Example:
object dataObject = LoadData(); DataSet ds = dataObject as DataSet;
if(ds != null)
{…}
Example:
int count = 1;
object refCount = count; // Implicitly boxed. int newCount = (int)refCount; // Explicitly unboxed.
Example: totalPercent = 0.05;
Example: (ToLower() createsa temp string)
// Bad!
int id = -1;
string name = "lance hunt";
for(int i=0; i < customerList.Count; i++)
{
if(customerList[i].Name.ToLower() == name)
{
id = customerList[i].ID;
}
}
// Good!
int id = -1;
string name = "lance hunt";
for(int i=0; i < customerList.Count; i++)
{
// The "ignoreCase = true" argument performs a
// case-insensitive compare without new allocation. if(String.Compare(customerList[i].Name, name, true)== 0)
{
id = customerList[i].ID;
}
}
Example: int result = isValid ? 9 : 4;
Example:
// Bad!
if (isValid == true)
{…}
// Good!
if (isValid)
{…}
Example:if((i=2)==2) {…}
Example:
// Bad!
if (((value > _highScore) && (value != _highScore)) && (value < _maxScore))
{…}
// Good!
isHighScore = (value >= _highScore); isTiedHigh = (value == _highScore); isValid = (value < _maxValue);
if ((isHighScore && ! isTiedHigh) && isValid)
{…}
Example:
// Bad!
if(IsValid == true)
{…};
// Good! if(IsValid)
{…}
statement.
Example:
// Bad! catch(Exception ex)
{
Log(ex); throw ex;
}
// Good! catch(Exception)
{
Log(ex); throw;
}
Example:
// Bad! try
{
conn.Close();
}
Catch(Exception ex)
{
// handle exception if already closed!
}
// Good!
if(conn.State != ConnectionState.Closed)
{
conn.Close();
}
public MyCustomException ();
public MyCustomException (string message);
public MyCustomException (string message, Exception innerException);
protected MyCustomException(SerializationInfo info, StreamingContext contxt);
to include custom property values.
Example:
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData (info, context);
info.AddValue("MyValue", _myValue);
}
Example:lock(myVariable);
Example:lock(typeof(MyClass));
Example:lock(this);
Example:
public void Test(BookCategory cat)
{
if (Enum.IsDefined(typeof(BookCategory), cat))
{…}
}
Example:
using(SqlConnection cn = new SqlConnection(_connectionString))
{…}
Example:(shownwithoptionalFinalizer)
public void Dispose()
{
Dispose(true); GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Free other state (managed objects).
}
// Free your own state (unmanaged objects).
// Set large fields to null.
}
// C# finalizer. (optional)
~Base()
{
// Simply call Dispose(false). Dispose (false);
}
Never define a Finalize() method as a finalizer. Instead use the C# destructor syntax.
Example
// Good
~MyClass {…}
// Bad
void Finalize(){…}
Refactor often!