Show Menu
Cheatography

C# naming convention Cheat Sheet (DRAFT) by

C# reference sheet for loosers

This is a draft cheat sheet. It is a work in progress and is not finished yet.

General Naming Conven­tions

Do
Don't
Use easily readable identifier names. Favor readab­ility over brevity.
Use unders­core, hyphen or Hungarian notation.
Use semant­ically intere­sting generic names. eg. GetAmo­untDue vs GetDec­ima­lValue
Use identi­fiers that conflict with C#
Use acronyms only if required, and only use widely accepted ones
Use abbrev­iations as part of the identi­fiers.

Parameters

Choose parameter names that indicate what data is being affected.
Good: firstName - Uses camel casing and is descri­ptive
Bad: decima­lSalary - Name should not be based on type

Enums

Do not use prefixes or suffixes
Usually names are plural nouns. E.g Teams, Colors
Do not use flag as suffix for the names of flag enumer­ations

Casing Rules

Identifier
Case
Example
Namespace
Pascal
System.Dr­awing
Class
Pascal
Customer
Class Field
Camel
_trans­fer­Config
Interface
Pascal
IPerson
Property
Pascal
Transf­erC­onfig
Method
Pascal
Transf­erA­ccount
Parameter
Camel
accoun­tNumber
Constant
Pascal
Infini­teT­hrottle
Enumer­ation Type
Pascal
Browse­rType
Enumer­ation Value
Pascal
Intern­etE­xplorer
Event
Pascal
Transf­erC­omplete
Exception Class
Pascal
Transf­erE­xce­ption
Pre-Pr­ocessor
Upper
NETSTA­NDARD

Resources

Nested identi­fiers with clear hierarchy. Ex: Menus.F­il­e.C­los­e.Text
 

Types

Fields
Typically nouns or noun phrases are used as names for the fields. e.g. _salary
Properties
Nouns, noun phrases or adjectives are used for naming properties
Properties and Get methods should not be named alike.
Boolean properties should be named with phrases like Is or Has.
Methods
Typically verbs or verb phrases are used as names for the methods. e.g. GetEnc­odi­ngS­tring()
Events
Typically verbs or verb phrases are used as names for the events.
In event handlers, use two parameter named sender and e.
Concept of before and after should be given, e.g Closing, Closed, etc

Classes, Structs, and Interfaces

Use pascal cased nouns, noun phrases or adjective phrases like Customer or Invoice. This distin­guishes type names from methods, which are named with verb phrases like SaveCu­stomer or LoadIn­voice.
Use of suffixes and prefixes
Derived class should have suffix repres­enting the base class. e,g OvalShape
Transf­erC­omp­let­eEv­ent­Handler – EventH­andler suffix for handlers
Transf­erC­omp­let­eCa­llback – Callback suffix to delegates
Transf­erE­xce­ption – Exception suffix for deriving from Exception
Accoun­tDi­cti­onary – Dictionary suffix for dictionary implem­ent­ations
Socket­Stream - Stream suffix for inheriting from System.IO.Stream
Do use the prefix I for Interf­aces. Example: ITransfer

Assemblies and DLL Names

Choose names that suggest large chunks of functi­ona­lity.
It is advisable if assembly and DLL names follow the namespace names.
The following pattern may be followed for naming DLLs:
<Co­mpa­ny>.<C­omp­one­nt>.dll
Where <component> contains one or more dot separated clauses.
Example: Compan­ySo­ft.J­so­n.dll