Naming Conventions
In this lesson, we will go through the naming conventions in C# as specified by Microsoft.
The Table
The following table enlists the case conventions to be followed for naming in C#:
Capitalization Styles
Pascal Casing
This convention capitalizes the first character of each word (as in TestCounter).
Camel Casing
This convention capitalizes the first character of each word except the first one. E.g. testCounter.
Upper case
Only use all upper case for identifiers if it consists of an abbreviation which is one or two characters long, identifiers of three or more characters should use Pascal Casing instead. For Example:
public class Math
{
public const PI = ...
public const E = ...
public const feigenBaumNumber = ...
}
Naming Guidelines
Generally, the use of underscore characters inside names and naming according to the guidelines for Hungarian notation are considered a bad practice.
Hungarian notation is a defined set of pre and postfixes which are applied to names to reflect the type of the variable.
This style of naming was widely used in early Windows programming, but now is obsolete or at least should be considered deprecated. Using Hungarian notation is not allowed if you follow this guide.
Remember: A good variable name describes the semantic not the type.
An exception to this rule is GUI code. All fields and variable names that contain GUI elements like button should be postfixed with their type name without abbreviations. For example:
System.Windows.Forms.Button cancelButton;
System.Windows.Forms.TextBox nameTextBox;
Class Naming
- Class names must be nouns or noun phrases.
- Use
PascalCasing
. - Do not use any class prefix.
Interface Naming
- Name interfaces with nouns or noun phrases or adjectives describing behavior (Example
IComponent
orIEnumberable
). - Use
PascalCasing
. - Use
I
as prefix for the name, it is followed by a capital letter (first char of the interface name).
Enum Naming
- Use
PascalCasing
for enum value names and enum type names. - Don’t prefix (or suffix) a enum type or enum values.
- Use singular names for enums.
- Use plural name for bit fields.
ReadOnly and Const Field Names
- Name
static
fields with nouns, noun phrases or abbreviations for nouns. - Use
PascalCasing
.
Parameter/non const field Names
- Do use descriptive names, which should be enough to determine the variable meaning and it’s type. But prefer a name that’s based on the parameter’s meaning.
- Use
camelCasing
.
Variable Names
- Counting variables are preferably called
i
,j
,k
,l
,m
,n
when used in ‘trivial’ counting loops. - Use
camelCasing
.
Method Names
- Name methods with verbs or verb phrases.
- Use
PascalCasing
.
Property Names
- Name properties using nouns or noun phrases.
- Use
PascalCasing
. - Consider naming a property with the same name as its type.
Event Names
- Name event handlers with the
EventHandler
suffix. - Use two parameters named
sender
ande
. - Use
PascalCasing
. - Name event argument classes with the
EventArgs
suffix. - Name event names that have a concept of pre and post using the present and past tense.
- Consider naming events using a verb.