November 20, 2006
Naming Conventions for C# / VB.NET Projects
This document explains the naming conventions that should be used with .NET projects.A consistent naming pattern is one of the most important elements of predictability and discoverability in a managed class library. Widespread use and understanding of these naming guidelines should eliminate unclear code and make it easier for developers to understand shared code.
Pascal case
The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized.
Example:
BackColor, DataSet
Camel case
The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.
Example:
numberOfDays, isValid
Naming Guidelines
1). Private Variables (Fields in C#) Naming Guidelines
Naming guidelines
Prefix private variables with a "_" and Hungarian-style notation.
Case guidelines
Use camel case as a general rule, or uppercase for very small words
Example:
_strFirstName, _dsetEmployees
// Field
private OleDbConnection _connection;
// Property
public OleDbConnection Connection
{
get { return _connection; }
set { _connection = value;}
}
2). Local Variables Naming Guidelines
Naming guidelines
Prefix private or local variables with Hungarian-style notation.
Case guidelines
Use camel case as a general rule, or uppercase for very small words
Example:
strFirstName, dsetEmployees
3). Namespace Naming Guidelines
Naming guidelines
The general rule for naming namespaces is to use the company name followed by the technology name and optionally the feature and design as follows:
CompanyName.TechnologyName[.Feature][.Design]
Prefixing namespace names with a company name or other well-established brand avoids the possibility of two published namespaces having the same name. Use a stable, recognized technology name at the second level of a hierarchical name.
Example:
Akadia.Traffic, System.Web.UI, System.Windows.Forms
Case guidelines
Use Pascal case as a general rule, or uppercase for very small words.
Example:
System.Windows.Forms, System.Web.UI
4). Class Naming Guidelines
Naming guidelines
Use a noun or noun phrase to name a class. Do not use a type prefix, such as C for class, on a class name.Do not use the underscore character (_).
Case guidelines
Use Pascal case. Example:
FileStream, Button
5). Interface Naming Guidelines
Naming guidelines
Prefix interface names with the letter "I", to indicate that the type is an interface.Do not use the underscore character (_).
Case guidelines
Use Pascal case. Example:
IServiceProvider, IFormatable
6). Parameter Naming Guidelines
Naming guidelines
Use descriptive parameter names. Parameter names should be descriptive enough that the name of the parameter and its type can be used to determine its meaning in most scenarios. To distinguish parameters from other variables the prefix "p" should be used.
Do not prefix parameter names with Hungarian type notation.
Do not use a prefix for parameter names of an event handler and exceptions.
Case guidelines
Use camel case. Example:
pTypeName, pNumberOfItems
7). Method Naming Guidelines
Naming guidelines
Use verbs or verb phrases to name methods.
Case guidelines
Use Pascal case. Example:
RemoveAll(), GetCharAt()
8). Property / Enumerations Naming Guidelines
Naming guidelines
Use a noun or noun phrase to name properties.Do not use Hungarian notation.
Case guidelines
Use Pascal case. Example:
BackColor, NumberOfItems
9). Event Naming Guidelines
Naming guidelines
Use an EventHandler suffix on event handler names.
Specify two parameters named sender and e. The sender parameter represents the object that raised the event. The sender parameter is always of type object, even if it is possible to use a more specific type. The state associated with the event is encapsulated in an instance of an event class named "e". Use an appropriate and specific event class for the e parameter type.
Name an event argument class with the EventArgs suffix.
Case guidelines
Use Pascal case. Example:
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
9). Exception Naming Guidelines
Naming guidelines
Event handlers in Visual Studio .NET tend to use an "e" parameter for the event parameter to the call. To ensure we avoid a conflict, we will use "ex" as a standard variable name for an Exception object.
Example
catch (Exception ex)
{
// Handle Exception
}
10). Constant Naming Guidelines
The names of variables declared class constants should be all uppercase with words separated by underscores. It is recommended to use a grouping naming schema.
Example (for group AP_WIN):
AP_WIN_MIN_WIDTH, AP_WIN_MAX_WIDTH, AP_WIN_MIN_HIGHT, AP_WIN_MAX_HIGHT
11). C# Primitive Type Notation
sbyte | sy |
short | s |
int | i |
long | l |
byte | y |
ushort | us |
uint | ui |
ulong | ul |
float | f |
double | d |
decimal | dec |
bool | b |
char | c |
12). Visual Control Type Notation
Assembly | asm |
Boolean | bln |
Button | btn |
Char | ch |
CheckBox | cbx |
ComboBox | cmb |
Container | ctr |
DataColumn | dcol |
DataGrid | dgrid |
DataGridDateTimePickerColumn | dgdtpc |
DataGridTableStyle | dgts |
DataGridTextBoxColumn | dgtbc |
DataReader | dreader |
DataRow | drow |
DataSet | dset |
DataTable | dtable |
DateTime | date |
Dialog | dialog |
DialogResult | dr |
Double | dbl |
Exception | ex |
GroupBox | gbx |
HashTable | htbl |
ImageList | iml |
Integer | int |
Label | lbl |
ListBox | lbx |
ListView | lv |
MarshallByRefObject | rmt |
Mainmenu | mm |
MenuItem | mi |
MDI-Frame | frame |
MDI-Sheet | sheet |
NumericUpDown | nud |
Panel | pnl |
PictureBox | pbx |
RadioButton | rbtn |
SDI-Form | form |
SqlCommand | sqlcom |
SqlCommandBuilder | sqlcomb |
SqlConnection | sqlcon |
SqlDataAdapter | sqlda |
StatusBar | stb |
String | str |
StringBuilder | strb |
TabControl | tabctrl |
TabPage | tabpage |
TextBox | tbx |
ToolBar | tbr |
ToolBarButton | tbb |
Timer | tmr |
UserControl | usr |
WindowsPrincipal | wpl |
1 Comments:
Your guidelines is partially in contradiction with Microsoft recommendations.
Microsoft does not recommend using underscores or Hungarian notation.
for private fields it is not very clear the notion of "very small words".
Never seen code using "p" prefix for the parameters. This became useless for usual small functions where local variables are not very numerous, elsewhere the method should be divided in submethods.
Post a Comment
<< Home