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 covers Naming Conventions, Coding Style, Language Usage, and Object Model Design. This document was adapted from the Lance Hunt .NET Coding Standards.
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.
The following terminology is referenced throughout this document:
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.
A word with the first letter lowercase, and the first letter of each subsequent word-part capitalized.
Example: customerName
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.
A developer defined token used to uniquely name a declared object or object instance.
Example:
public class MyClassNameIdentifier { … }
Any numeric literal used within an expression (or to initialize a variable) that does not have an obvious or wellknown meaning. This usually excludes the integers 0 or 1 and any other numeric equivalent precision that evaluates as zero.
A word with the first letter capitalized, and the first letter of each subsequent word-part capitalized.
Example: CustomerName
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.”
“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 | P | P | P | P | Use T or K as Type identifier. |
Method | P | P | P | P | 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. |
Indentation | Use spaces with size of 4. |
Comments | Use // or /// but not /* … */ and do not flowerbox. |
Variables | One variable per declaration. |
Native Data Types | Use built-in C# native data types vs .NET CTS types. (Use int NOT Int32 ) |
Enums | Avoid changing default type. |
Generics | Prefer Generic Types over standard or strong-typed classes. |
Properties | Never prefix with Get or Set . |
Methods | Use a maximum of 7 parameters. |
base and this | Use only in constructors or within an override. |
Ternary conditions | Avoid complex conditions. |
foreach statements | 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.
-
Always use Camel Case or Pascal Case names.
-
Avoid ALL CAPS and all lowercase names. Single lowercase words or letters are acceptable.
-
Do not create declarations of the same type (namespace, class, method, property, field, or parameter) and access modifier (
protected
,public
,private
,internal
) that vary only by capitalization. -
Do not use names that begin with a numeric character.
-
Do add numeric suffixes to identifier names.
-
Always choose meaningful and specific names.
-
Always err on the side of verbosity not terseness.
-
Variables and Properties should describe an entity not the type or size.
-
Do not use Hungarian Notation! Example:
strName
oriCount
-
Avoid using abbreviations unless the full name is excessive.
-
Avoid abbreviations longer than 5 characters.
-
Any Abbreviations must be widely known and accepted.
-
Use uppercase for two-letter abbreviations, and Pascal Case for longer abbreviations.
-
Do not use C# reserved words as names.
-
Avoid naming conflicts with existing .NET Framework namespaces, or types.
-
Avoid adding redundant or meaningless prefixes and suffixes to identifiers
Example:
// Bad! public enum ColorsEnum {…} public class CVehicle {…} public struct RectangleStruct {…}
-
Do not include the parent class name within a property name.
Example:
Customer.Name
NOTCustomer.CustomerName
-
Try to prefix Boolean variables and properties with “
Can
”, “Is
” or “Has
”. -
Append computational qualifiers to variable names like
Average
,Count
,Sum
,Min
, andMax
where appropriate. -
When defining a root namespace, use a Product, Company, or Developer Name as the root.
Example:
My.StringUtilities
Identifier | Naming Convention |
---|---|
Project File | Pascal Case. Always match Assembly Name & Root Namespace. Example: My.Web.csproj -> My.Web.dll -> namespace My.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 Embedded File | Try to use Pascal Case. Use a name describing the file contents. |
Namespace | Pascal Case. Try to partially match Project/Assembly Name. Example: namespace My.Web {…} |
Class or 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 {…} |
Generic Class & Generic Parameter Type | Always use a single capital letter, such as T or K . Example: public class FifoStack<T> |
Method | Pascal Case. Try to use a Verb or Verb-Object pair. 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 |
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; |
Constant or Static Field | 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 } |
Delegate or Event | 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.
-
Never declare more than 1 namespace per file.
-
Avoid putting multiple classes in a single file.
-
Always place curly braces (
{
and}
) on a new line. -
Always use curly braces (
{
and}
) in conditional statements. -
Always use Spaces & Indentation size of
4
. -
Declare each variable independently – not in the same statement.
-
Place namespace “
using
” statements together at the top of file. Group .NET namespaces above custom namespaces. -
Group internal class implementation by type in the following order:
- Member variables.
- Constructors & Finalizers.
- Nested Enums, Structs, and Classes.
- Properties
- Methods
-
Sequence declarations within type groups based upon access modifier and visibility:
- Public
- Protected
- Internal
- Private
-
Segregate interface Implementation by using
#region
statements. -
Append folder-name to namespace for source files within sub-folders.
-
Recursively indent all code blocks contained within braces.
-
Use white space (CR/LF, Indentation, etc) liberally to separate and organize code.
-
Only declare related
attribute
declarations on a single line, otherwise stack each attribute as a separate declaration.Example:
// Bad! [Attrbute1, Attrbute2, Attrbute3] public class MyClass {…} // Good! [Attrbute1, RelatedAttribute2] [Attrbute3] [Attrbute4] public class MyClass {…}
-
Place Assembly scope
attribute
declarations on a separate line. -
Place Type scope
attribute
declarations on a separate line. -
Place Method scope
attribute
declarations on a separate line. -
Place Member scope
attribute
declarations on a separate line. -
Place Parameter
attribute
declarations inline with the parameter. -
If in doubt, always err on the side of clarity and consistency.
-
All comments should be written in the same language, be grammatically correct, and contain appropriate punctuation.
-
Use
//
or///
but never/* … */
-
Do not “flowerbox” comment blocks.
Example:
// *************************************** // Comment block // ***************************************
-
Use inline-comments to explain assumptions, known issues, and algorithm insights.
-
Do not use inline-comments to explain obvious code. Well written code is self documenting.
-
Only use comments for bad code to say “fix this code” – otherwise remove, or rewrite the code!
-
Include comments using Task-List keyword flags to allow comment-filtering.
Example:
// TODO: Place Database Code Here // UNDONE: Removed P\Invoke Call due to errors // HACK: Temporary fix until able to refactor
-
Always apply C# comment-blocks (
///
) topublic
,protected
, andinternal
declarations. -
Only use C# comment-blocks for documenting the API.
-
Always include
<summary>
comments. Include<param>
,<return>
, and<exception>
comment sections where applicable. -
Include
<see cref=""/>
and<seeAlso cref=""/>
where possible. -
Always add CDATA tags to comments containing code and other embedded markup in order to avoid encoding issues.
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>
-
Do not omit access modifiers. Explicitly declare all identifiers with the appropriate access modifier instead of allowing the default.
Example:
// Bad! Void WriteEvent(string message) {…} // Good! private Void WriteEvent(string message) {…}
-
Do not use the default (“1.0.*”) versioning scheme. Increment the
AssemblyVersionAttribute
value manually. -
Set the
ComVisibleAttribute
tofalse
for all assemblies. -
Only selectively enable the
ComVisibleAttribute
for individual classes when needed.Example:
[assembly: ComVisible(false)] [ComVisible(true)] public MyClass {…}
-
Consider factoring classes containing
unsafe
code blocks into a separate assembly. -
Avoid mutual references between assemblies.
-
Try to initialize variables where you declare them.
-
Always choose the simplest data type, list, or object required.
-
Always use the built-in C# data type aliases, not the .NET common type system (CTS).
Example:
short NOT System.Int16 int NOT System.Int32 long NOT System.Int64 string NOT System.String
-
Only declare member variables as
private
. Use properties to provide access to them withpublic
,protected
, orinternal
access modifiers. -
Try to use
int
for any non-fractional numeric values that will fit theint
datatype - even variables for nonnegative numbers. -
Only use
long
for variables potentially containing values too large for anint
. -
Try to use
double
for fractional numbers to ensure decimal precision in calculations. -
Only use
float
for fractional numbers that will not fitdouble
ordecimal
. -
Avoid using
float
unless you fully understand the implications upon any calculations. -
Try to use
decimal
when fractional numbers must be rounded to a fixed precision for calculations. Typically this will involve money. -
Avoid using
sbyte
,short
,uint
, andulong
unless it is for interop (P/Invoke) with native libraries. -
Avoid specifying the type for an
enum
- use the default ofint
unless you have an explicit need forlong
(very uncommon). -
Avoid using inline numeric literals (magic numbers). Instead, use a
Constant
orEnum
. -
Avoid declaring string literals inline. Instead use Resources, Constants, Configuration Files, Registry or other data sources.
-
Declare
readonly
orstatic readonly
variables instead of constants for complex types. -
Only declare
constants
for simple types. -
Avoid direct casts. Instead, use the “
as
” operator and check fornull
.Example:
object dataObject = LoadData(); DataSet ds = dataObject as DataSet; if(ds != null) {…}
-
Always prefer C# Generic collection types over standard or strong-typed collections.
-
Always explicitly initialize arrays of reference types using a
for
loop. -
Avoid boxing and unboxing value types.
Example:
int count = 1; object refCount = count; // Implicitly boxed. int newCount = (int)refCount; // Explicitly unboxed.
-
Floating point values should include at least one digit before the decimal place and one after.
Example: totalPercent = 0.05;
-
Try to use the “
@
” prefix for string literals instead of escaped strings. -
Prefer
String.Format()
orStringBuilder
over string concatenation. -
Never concatenate strings inside a loop.
-
Do not compare strings to
String.Empty
or""
to check for empty strings. Instead, compare by usingString.Length == 0
. -
Avoid hidden string allocations within a loop. Use
String.Compare()
for case-sensitiveExample: (
ToLower()
creates a temp string)// Bad! int id = -1; string name = "john doe"; for (int i=0; i < customerList.Count; i++) { if (customerList[i].Name.ToLower() == name) { id = customerList[i].ID; } } // Good! int id = -1; string name = "john doe"; 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; } }
-
Avoid invoking methods within a conditional expression.
-
Avoid creating recursive methods. Use loops or nested loops instead.
-
Avoid using
foreach
to iterate over immutable value-type collections. E.g. String arrays. -
Do not modify enumerated items within a
foreach
statement. -
Use the ternary conditional operator only for trivial conditions. Avoid complex or compound ternary operations.
Example:
int result = isValid ? 9 : 4;
-
Avoid evaluating Boolean conditions against true or false.
Example:
// Bad! if (isValid == true) {…} // Good! if (isValid) {…}
-
Avoid assignment within conditional statements.
Example:
if((i=2)==2) {…}
-
Avoid compound conditional expressions – use Boolean variables to split parts into multiple manageable expressions.
Example:
// Bad! if (((value > _highScore) && (value != _highScore)) && (value < _maxScore)) {…} // Good! isHighScore = (value >= _highScore); isTiedHigh = (value == _highScore); isValid = (value < _maxValue); if ((isHighScore && ! isTiedHigh) && isValid) {…}
-
Avoid explicit Boolean tests in conditionals.
Example:
// Bad! if (IsValid == true) {…}; // Good! if (IsValid) {…}
-
Only use
switch
/case
statements for simple operations with parallel conditional logic. -
Prefer nested
if
/else
overswitch
/case
for short conditional sequences and complex conditions. -
Prefer polymorphism over
switch
/case
to encapsulate and delegate complex operations.
-
Do not use
try
/catch
blocks for flow-control. -
Only
catch
exceptions that you can handle. -
Never declare an empty
catch
block. -
Avoid nesting a
try
/catch
within acatch
block. -
Always catch the most derived exception via exception filters.
-
Order exception filters from most to least derived exception type.
-
Avoid re-throwing an exception. Allow it to bubble-up instead.
-
If re-throwing an exception, preserve the original call stack by omitting the exception argument from the
throw
statement.Example:
// Bad! catch (Exception ex) { Log(ex); throw ex; } // Good! catch (Exception ex) { Log(ex); throw; }
-
Only use the
finally
block to release resources from atry
statement. -
Always use validation to avoid exceptions.
Example:
// Bad! try { conn.Close(); } catch (Exception ex) { // handle exception if already closed! } // Good! if (conn.State != ConnectionState.Closed) { conn.Close(); }
-
Always set the
innerException
property on thrown exceptions so the exception chain & call stack are maintained. -
Avoid defining custom exception classes. Use existing exception classes instead.
-
When a custom exception is required;
-
Always derive from
Exception
notApplicationException
. -
Always suffix exception class names with the word “Exception”.
-
Always add the
SerializableAttribute
to exception classes. -
Always implement the standard “Exception Constructor Pattern”:
public MyCustomException (); public MyCustomException (string message); public MyCustomException (string message, Exception innerException);
-
Always implement the deserialization constructor:
protected MyCustomException(SerializationInfo info, StreamingContext contxt);
-
-
Always set the appropriate HResult value on custom exception classes. (Note: the ApplicationException HResult = -2146232832)
-
When defining custom exception classes that contain additional properties:
-
Always override the
Message
property,ToString()
method and theimplicit operator string
to include custom property values. -
Always modify the deserialization constructor to retrieve custom property values.
-
Always override the GetObjectData(…) method to add custom properties to the serialization collection.
Example:
public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); info.AddValue("MyValue", _myValue); }
-
-
Always check Event & Delegate instances for
null
before invoking. -
Use the default
EventHandler
andEventArgs
for most simple events. -
Always derive a custom
EventArgs
class to provide additional data. -
Use the existing
CancelEventArgs
class to allow the event subscriber to control events. -
Always use the “
lock
” keyword instead of theMonitor
type. -
Only
lock
on a private or private static object.Example:
lock(myVariable);
-
Avoid locking on a Type.
Example:
lock(typeof(MyClass));
-
Avoid locking on the current object instance.
Example:
lock(this);
-
Always declare types explicitly within a namespace. Do not use the default “{global}” namespace.
-
Avoid overuse of the
public
access modifier. Typically fewer than 10% of your types and members will be part of a public API, unless you are writing a class library. -
Consider using
internal
orprivate
access modifiers for types and members unless you intend to support them as part of a public API. -
Never use the
protected
access modifier withinsealed
classes unless overriding aprotected
member of an inherited type. -
Avoid declaring methods with more than
5
parameters. Consider refactoring this code. -
Try to replace large parameter-sets (> than
5
parameters) with one or moreclass
orstruct
parameters – especially when used in multiple method signatures. -
Do not use the “
new
” keyword on method and property declarations to hide members of a derived type. -
Only use the “
base
” keyword when invoking a base class constructor or base implementation within an override. -
Consider using method overloading instead of the
params
attribute (but be careful not to break CLS Compliance of your API’s). -
Always validate an enumeration variable or parameter value before consuming it. They may contain any value that the underlying Enum type (default
int
) supports.Example:
public void Test(BookCategory cat) { if (Enum.IsDefined(typeof(BookCategory), cat)) {…} }
-
Consider overriding
Equals()
on astruct
. -
Always override the
Equality Operator (==)
when overriding theEquals()
method. -
Always override the
String Implicit Operator
when overriding theToString()
method. -
Always call
Close()
orDispose()
on classes that offer it. -
Wrap instantiation of
IDisposable
objects with a “using
” statement to ensure thatDispose()
is automatically called.Example:
using (SqlConnection cn = new SqlConnection(_connectionString)) {…}
-
Always implement the
IDisposable
interface & pattern on classes referencing external resources.Example: (shown with optional Finalizer)
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); }
-
Avoid implementing a Finalizer. Never define a
Finalize()
method as a finalizer. Instead use the C# destructor syntax.Example:
// Good ~MyClass {…} // Bad void Finalize() {…}
- Always prefer aggregation over inheritance.
- Avoid “Premature Generalization”. Create abstractions only when the intent is understood.
- Do the simplest thing that works, then refactor when necessary.
- Always make object-behavior transparent to API consumers.
- Avoid unexpected side-affects when properties, methods, and constructors are invoked.
- Always separate presentation layer from business logic.
- Always prefer interfaces over abstract classes.
- Try to include the design-pattern names such as “Bridge”, “Adapter”, or “Factory” as a suffix to class names where appropriate.
- Only make members
virtual
if they are designed and tested for extensibility. - Refactor often!
“Coding Standards For .NET”, Lance Hunt, March 2007, http://www.myp11d.com/p11d/download/policies/csharp_coding_standards.pdf
“MSDN: .NET Framework Developer’s Guide: Common Type System”, Microsoft Corporation, 2004, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconthecommontypesystem.asp
“MSDN: C# Language Specification v1.5”, Scott Wiltamuth & Anders Hejlsberg, Microsoft Corporation, 2003, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_15.asp
“MSDN: Design Guidelines for Class Library Developers”, Microsoft Corporation, 2004, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconNETFrameworkDesignGuidelines.asp
“MSDN: The Well Tempered Exception”, Eric Gunnerson, Microsoft Corporation, 2001 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp08162001.asp
“Applied Microsoft .NET Framework Programming”, Jeffrey Richter, January 23, 2002, 1st ed., Microsoft Press, ISBN: 0735614229
“Which type should I use in C# to represent numbers?”, locabol, February 27, 2007, Luca Bolognese’s Weblog http://blogs.msdn.com/lucabol/archive/2007/02/27/which-type-should-i-use-in-c-to-represent-numbers.aspx