C# Programming/Naming

This section will define the naming conventions that are generally accepted by the C# development community. Some companies may define naming conventions that differ from this, but that is done on an individual basis and is generally discouraged. Some of the objects discussed in this section may be beyond the reader's knowledge at this point, but this section can be referred back to later.

ReasoningEdit

Much of the naming standards are derived from Microsoft's .NET Framework libraries. These standards have proven to make names readable and understandable "at a glance". By using the correct conventions when naming objects, you ensure that other C# programmers who read your code will easily understand what objects are without having to search your code for their definition.

ConventionsEdit

NamespaceEdit

Namespaces are named using Pascal Case (also called UpperCamelCase) with no underscores. This means the first letter of every word in the name is capitalized. For example: MyNewNamespace. Also, note that Pascal Case also denotes that acronyms of three or more letters should only have the first letter capitalized (MyXmlNamespace instead of MyXMLNamespace).

AssembliesEdit

If an assembly contains only one namespace, they should use the same name. Otherwise, Assemblies should follow the normal Pascal Case format.

Classes and StructuresEdit

Pascal Case, no underscores or leading C, cls, or I. Classes should not have the same name as the namespace in which they reside. Any acronyms of three or more letters should be pascal case, not all caps. Try to avoid abbreviations, and try to always use nouns.

Exception ClassesEdit

Follow class naming conventions, but add Exception to the end of the name. In .Net 2.0, all classes should inherit from the System.Exception base class, and not inherit from the System.ApplicationException.

InterfacesEdit

Follow class naming conventions, but start the name with I and capitalize the letter following the I. Example: IFoo The I prefix helps to differentiate between Interfaces and classes and also to avoid name collisions.

FunctionsEdit

Pascal Case, no underscores except in the event handlers. Try to avoid abbreviations. Many programmers have a nasty habit of overly abbreviating everything. This should be discouraged.

Properties and Public Member VariablesEdit

Pascal Case, no underscores. Try to avoid abbreviations.

Parameters and Procedure-level VariablesEdit

Camel Case (or lowerCamelCase). Try to avoid abbreviations. Camel Case is the same as Pascal case, but the first letter of the first word is lowercased.

Class-level Private and Protected VariablesEdit

Camel Case with a leading underscore. Always indicate protected or private in the declaration. The leading underscore is the only controversial thing in this document. The leading character helps to prevent name collisions in constructors (a parameter and a private variable having the same name).

Controls on FormsEdit

Pascal Case with a prefix that identifies it as being part of the UI instead of a purely coded control (example a temporary variable). Many developers use ui as the prefix followed by a descriptive name such as txtUserName or lblUserNickName ("txt" stands for TextBox control and "lbl" for Label control)

Some samples are below for ASP.Net web form controls:

Control Prefix Example
Label lbl lblSurname
TextBox txt txtSurname
DataGrid dg dgResults
GridView gv gvResults2
Button btn btnSave
ImageButton iBtn iBtnSave
Hyperlink lnk lnkHomePage
DropDownList ddl ddlCompany
ListBox lst lstCompany
DataList dLst dlstAddress
DataSet ds dsInvoices
DataTable dt dtClients
DataRow dr drUser
Repeater rep repSection
Checkbox chk chkMailList
CheckBoxList chk chkAddress
RadioButton rBtn rBtnSex
RadioButtonList rBtn rBtnAgeGroup
Image img imgLogo
Panel pnl pnlSevtion
PlaceHolder plh plhHeader
Calendar txt txtMyDate
AdRotator adr adrBanner
Table tbl tblResults
[All] Validators val (N/A) valCreditCardNumber
ValidationSummary vals (N/A) valsErrors

ConstantsEdit

Pascal Case. The use of SCREAMING_CAPS is discouraged. This is a large change from earlier conventions. Most developers now realize that in using SCREAMING_CAPS they betray more implementation than is necessary. A large portion of the .NET Framework Design Guidelines is dedicated to this discussion.

ExampleEdit

Here is an example of a class that uses all of these naming conventions combined.

using System;
 
namespace MyExampleNamespace
{
    public class Customer : IDisposable
    {
        private string _customerName;
        public string CustomerName 
        { 
            get 
            { 
                return _customerName; 
            }
            set
            {
                _customerName = value;
                _lastUpdated = DateTime.Now;
            }
        }
 
        private DateTime _lastUpdated;
 
        public DateTime LastUpdated
        {
            get
            {
                return _lastUpdated;
            }
            private set
            {
                _lastUpdated = value;
            }
        }
 
        public void UpdateCustomer(string newName)
        {
            if (!newName.Equals(CustomerName))
            {
                CustomerName = newName;
            }
        }
 
        public void Dispose()
        {
            //Do nothing
        }
    }
}


Last modified on 2 April 2014, at 13:24