Show Menu
Cheatography

C# Naming Conventions Cheat Sheet by

C# Naming Conventions based on https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines

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
Except for parame­ters, class fields, and pre-pr­ocessor direct­ives, all other identi­fiers follow a Pascal naming conven­tion.

Namespaces

Choose names that indicate functi­onality
The general format for a namespace name is as follows:
<Co­mpa­ny>.(<­Pro­jec­t>)­[.<­Fea­tur­e>]­[.<­Sub­nam­esp­ace­>]
Example: Newton­Sof­t.J­son.Linq

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 <co­mpo­nen­t> contains one or more dot separated clauses.
Example: Newton­Sof­t.J­son.dll

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

Resources

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

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
 

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.

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

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
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Regular Expressions Cheat Sheet
          Python Cheat Sheet

          More Cheat Sheets by GregFinzer

          Salesforce CLI Cheat Sheet
          Angular CLI Cheat Sheet
          Elasticsearch Example Queries Cheat Sheet