C# Coding Standards, Guidelines, Best Practices, and Other Buzzwords

This document is a work in progress and will be updated periodically.

Last revised on Jan 2008 - v1.00.01

Table of contents

  1. Terminology
  2. Namespaces
  3. Classes and Structs
  4. Collection Classes
  5. Delegate Classes
  6. Exception Classes
  7. Attribute Classes
  8. Interfaces
  9. Methods and Properties
  10. Enumerations
  11. Parameters
  12. UI Controls
  13. Member Variables
  14. Local Variables
  15. Constants
  16. Assemblies
  17. Folder and File Names
  18. URLs, Query Strings, and Cookies
  19. The Others

1. Terminology

Pascal Case
The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. You can use Pascal case for identifiers of three or more characters. For example: FajitaBurrito
Camel Case
The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example: fajitaBurrito

2. Namespaces

Standard / Convention Example
  • Smile at - using Pascal case for namespaces
  • Smile at - using CompanyName.TechnologyName or DomainName.TechnologyName when naming library namespaces
  • Smile at - using CompanyName.FolderName or DomainName.FolderName when naming web page namespaces
  • Smile at - placing all framework namespaces above any custom or third-party namespaces - separating each grouping with an empty line
  • Smile at - using standard "using" and "aliased using" statements to prevent fully qualified type names from making your code less readable
  • Frown at - placing "using" statements below the namespace statement
  • Frown at - using underscores in namespaces
BurritoHut.General

3. Classes and Structs

Standard / Convention Example
  • Smile at - using Pascal case for class and struct names
  • Smile at - naming the file that contains a class the same as the containing class name
  • Frown at - placing more than one class per file - there can be exceptions for highly related classes that would have conflicting naming if placed in a separate file
  • Frown at - placing a class in more than one namespace
Burrito
FajitaBurrito

4. Collection Classes

Standard / Convention Example
  • Smile at - suffixing custom extended System.Collection classes with the word "Collection"
BurritoCollection

5. Delegate Classes

Standard / Convention Example
  • Smile at - suffixing custom delegate classes with the word "Delegate"
BurritoDelegate

6. Exception Classes

Standard / Convention Example
  • Smile at - suffixing custom exception classes with the word "Exception"
BurritoException

7. Attribute Classes

Standard / Convention Example
  • Smile at - suffixing custom attribute classes with the word "Attribute"
BurritoAttribute

8. Interfaces

Standard / Convention Example
  • Smile at - prefixing interface names with "I"
IBurrito

9. Methods and Properties

Standard / Convention Example
  • Smile at - using Pascal case for method and property names
  • Smile at - naming methods using verb-object pair, such as ShowBurrito()
  • Smile at - naming methods with return values describing the value returned, such as GetBurrito()
  • Smile at - discouraging abbreviations
  • Smile at - marking public and protected methods as virtual in non-sealed classes
  • Frown at - using underscores "_" except for event handlers
public virtual void ShowBurrito()

protected void Burrito_Eat()

10. Enumerations

Standard / Convention Example
  • Smile at - ending Enum statements with a plural name when the enumeration represents a set of bitwise flags - otherwise use signlar naming
  • Smile at - suffixing enumerations with the words like "Type", "State", "Format", "Status", and "Option"
SauceTypes (bitwise flags)
BurritoType (standard enum)

11. Parameters

Standard / Convention Example
  • Smile at - using camel case for parameters
GetFajitaBurrito(int fajitaBurritoID)

12. Member Variables

Standard / Convention Example
  • Smile at - using camel case for private and protected member variable names
  • Smile at - using Pascal case for public member variable names - um, who exposes public member variables anyway - use property procedures
  • Smile at - prefixing "_" (underscore) to all private and protected member variables (CLS-compliance requires public and protected member variables to begin with a character other than an underscore - use "m_" (m underscore) if you require CLS-compliance)
  • Frown at - using underscores after the prefixed underscore or "m underscore" in member variable names
  • Frown at - using public member variables - replace with property procedures
public int EvilMemberVariable

protected int FajitaBurritoID

private int _burritoID

13. Local Variables

Standard / Convention Example
  • Smile at - using camel case for local variable names
int fajitaBurrito

14. Constants

Standard / Convention Example
  • Smile at - using the same naming conventions as non-constant variables with the same scope
  • Smile at - suffixing constants with the word "Constant"
Burrito.MaxBurritosPerSecondConstant

private int _maxBurritosPerSecondConstant

int localMaxBurritosPerSecondConstant

15. UI Controls

Standard / Convention Example
  • Smile at - prefixing UI control identifiers with "UX" - for User Experience - this is an exception to the normal private member variable naming - old Cascading Style Sheet (CSS) standards don't like identifiers that start with an underscore
  • Frown at - specifying the type name of a UI control in the identifier name
public TextBox UXEvilMemberBurritoName;

protected TextBox UXBurritoName;

private TextBox uxBurritoName;

16. Assemblies

Standard / Convention Example
  • Smile at - using Pascal case for assemblies
  • Smile at - using the same name as the root namespace
BurritoHut.WebPlatform.dll

17. Folders and File Names

Standard / Convention Example
  • Smile at - using Pascal case for folders and file names - while keeping file extensions in all lowercase
  • Frown at - using underscores "_" in folder names - if you feel you need to use something to separate words in a folder name - can you say SEO - then use a hyphen "-"
  • Frown at - using underscores "_" in file names - if you feel you need to use something to separate words in a file name - and the file does not use the .NET framework i.e. Html, Js, Css - then use a hyphen "-"
/Burrito-Standard/

BurritoHut-Min.css

18. URLs, Query Strings, and Cookies

Standard / Convention Example
  • Smile at - referencing all URLs that exist in Html or being redirected i.e Response.Redirect() using all lowercase characters
  • Smile at - using camel case query string item names
  • Smile at - using camel case cookie item names
  • Smile at - using the standard & (ampersand) as the query string separator or the ; (semicolon) - just agree on one and stick with it
/css/burritohut-min.css

/products/burrito/edit.aspx?burritoID=39

19. The Others

Standard / Convention
  • Smile at - creating identifiers made up of words with clear meaning
  • Smile at - discouraging abbreviations
  • Smile at - using abbreviations of three or more characters using Pascal case - only two character abbreviations should both be capatilized
  • Smile at - declaring local variables as close as possible to their first use
  • Smile at - placing open "{" and close "}" curly brackets on new lines
  • Smile at - placing all member variables at the top of each class, starting with the broadest scope down to the least (public, protected, private), with one empty line separating each similar scrope grouping
  • Smile at - validating parameters and throwing invalid values - use throw over Debug.Assert
  • Smile at - using Debug.Assert for every assumption in your code - except validating paramters
  • Smile at - using String.Empty instead of ""
  • Smile at - using String.IsNullOrEmpty instead of == null || == ""
  • Smile at - throwing an exception for unimplemented switch and if-else statements
  • Smile at - using 4 spaces for each level of indention throughout the code - this is the default in Visual Studio
  • Frown at - using an "_" (underscore), "-" (hyphen) or "m_" (m underscore)to any identifier other than private and protected member variables
  • Frown at - using Hungarian notation
  • Frown at - creating variable names such as "i" instead of "index"
  • Frown at - using identifiers whose names only differ by upper or lower case characters
  • Frown at - creating identifier names that contain abbreviations unless very well known i.e. "ID" - favor clarity over abbreviations
  • Frown at - suffixing identifiers with the word "Type" unless you are using a real .NET type of Type
  • Frown at - catching exceptions that you are not explicitly handling
  • Frown at - using public and protected member variables outside of the class - use properties instead
  • Frown at - using late-binding unless as a last resort