C# - Coding Standards
C# - Coding Standards
C# - Coding Standards
1. Introduction
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.
1.1 Scope
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.
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
Identifier
1 CS3020: C# Coding Standard for .NET
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
wellknown 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.”
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.
Enums
Generics
Prefer Generic Types over standard or strong-typed classes.
Properties
Never prefix with Get or Set.
Methods
Use a maximum of 7 parameters.
Ternary conditions
Avoid complex conditions.
foreach
Do not modify enumerated items within a foreach statement.
Conditionals
o Avoid evaluating Boolean conditions against true or false.
o No embedded assignment.
o Avoid embedded method invocation.
Exceptions
o Do not use exceptions for flow control.
o Use throw; not throw e; when re-throwing.
o Only catch what you can handle.
o Use validation to avoid exceptions.
o Derive from Execption not ApplicationException.
Events
Always check for null before invoking.
Locking
o Use lock() not Monitor.Enter().
o Do not lock on an object type or “this”.
o Do lock on private objects.
Finalizers
o Avoid.
o Use the C# Destructors.
o Do not create Finalize() method.
AssemblyVersion
Increment manually.
ComVisibleAttribute
Set to false for all assemblies.
2. Naming Conventions
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.
Source File
o Pascal Case.
o Always match Class name and file name.
o 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
{…}
Namespace
o Pascal Case.
o Try to partially match Project/Assembly Name.
Example:
namespace DotNET.Web
5 CS3020: C# Coding Standard for .NET
{…}
Class or Struct
o Pascal Case.
o Use a noun or noun phrase for class name.
o Add an appropriate class-suffix when sub-classing another type when possible.
Interface
o Pascal Case.
o Always prefix interface name with capital “I”.
Example:
interface ICustomer
{…}
Method
o Pascal Case.
o Try to use a Verb or Verb-Object pair.
Example:
public void Execute() {…}
private string GetAssemblyVersion(Assembly target) {…}
Property
o Pascal Case.
o Property name should represent the entity it returns. Never prefix property names with “Get” or
“Set”.
Example:
public string Name
{
get{…}
set{…}
Field (Private)
o Camel Case and prefix with a single underscore (_) character.
Example:
private string _name;
Enum
o Pascal Case (both the Type and the Options).
o Add the FlagsAttribute to bit-mask multiple options.
Example:
public enum CustomerTypes
{
Consumer,
Commercial
}
Delegate or Event
o Treat as a Field.
o Choose appropriate Field access-modifier above.
Example:
public event EventHandler LoadPlugin;
Variable (inline)
o Camel Case.
o Avoid using single characters like “x” or “y” except in FOR loops.
o Avoid enumerating variable names like text1, text2, text3 etc.
Parameter
7 CS3020: C# Coding Standard for .NET
o Camel Case.
Example:
public void Execute(string commandText, int iterations)
{…}
3. Coding Style
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 the key points
to create 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.
3.1 Formatting
1. Never declare more than one namespace per file.
2. Avoid putting multiple classes in a single file.
3. Always place curly braces ({ and }) on a new line.
4. Always use curly braces ({ and }) in conditional statements.
5. Always use a Tab & Indention size of 4.
6. Declare each variable independently – not in the same statement.
7. Place namespace “using” statements together at the top of file. Group .NET namespaces above custom
namespaces.
8. Group internal class implementation by type in the following order:
a. Member variables.
b. Constructors & Finalizers.
c. Nested Enums, Structs, and Classes.
d. Properties
e. Methods
9. Sequence declarations within type groups based upon access modifier and visibility:
a. Public
b. Protected
c. Internal
d. Private
10. Segregate interface Implementation by using #region statements.
11. Append folder-name to namespace for source files within sub-folders.
12. Recursively indent all code blocks contained within braces.
13. Use white space (CR/LF, Tabs, etc) liberally to separate and organize code.
14. Only declare related attribute declarations on a single line, otherwise stack each attribute as a separate
declaration.
Example:
// Bad!
8 CS3020: C# Coding Standard for .NET
[Attrbute1, Attrbute2, Attrbute3]
public class MyClass
{…}
// Good!
[Attrbute1, RelatedAttribute2]
[Attrbute3]
[Attrbute4]
public class MyClass
{…}
24. Use inline-comments to explain assumptions, known issues, and algorithm insights.
25. Do not use inline-comments to explain obvious code. Well written code is self documenting.
26. Only use comments for bad code to say “fix this code” – otherwise remove, or rewrite the code!
27. 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
28. Always apply C# comment-blocks (///) to public, protected, and internal declarations.
29. Only use C# comment-blocks for documenting the API.
30. Always include <summary> comments. Include <param>, <return>, and <exception> comment sections
where applicable.
31. Include <see cref=””/> and <seeAlso cref=””/> where possible.
4. Language Usage
4.1 General
1. 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)
{…}
2. Do not use the default (“1.0.*”) versioning scheme. Increment the AssemblyVersionAttribute value
manually.
3. Set the ComVisibleAttribute to false for all assemblies.
4. Only selectively enable the ComVisibleAttribute for individual classes when needed.
Example:
[assembly: ComVisible(false)]
[ComVisible(true)]
public MyClass
{…}
5. Consider factoring classes containing unsafe code blocks into a separate assembly.
6. Avoid mutual references between assemblies.
4.4 Exceptions
45. Do not use try/catch blocks for flow-control.
46. Only catch exceptions that you can handle.
47. Never declare an empty catch block.
48. Avoid nesting a try/catch within a catch block.
49. Always catch the most derived exception via exception filters.
50. Order exception filters from most to least derived exception type.
51. Avoid re-throwing an exception. Allow it to bubble-up instead.
13 CS3020: C# Coding Standard for .NET
52. 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)
{
Log(ex);
throw;
}
53. Only use the finally block to release resources from a try statement.
54. 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();
}
55. Always set the innerException property on thrown exceptions so the exception chain & call stack are
maintained.
56. Avoid defining custom exception classes. Use existing exception classes instead.
57. When a custom exception is required;
a. Always derive from Exception not ApplicationException.
b. Always suffix exception class names with the word “Exception”.
c. Always add the SerializableAttribute to exception classes.
d. Always implement the standard “Exception Constructor Pattern”:
public MyCustomException ();
public MyCustomException (string message);
83. 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).
Deepak Gupta
Course Co-ordinator