Naming conventions

C Sharp Programming · Wikibooks

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From Wikibooks, under CC BY-SA 4.0, as the source states it. This page at Wikibooks.

    Naming conventions

    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.

    Reasoning

    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.

    Conventions

    Namespace

    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. Pascal Case also means that acronyms of three or more letters should only have the first letter capitalized (MyXmlNamespace instead of MyXMLNamespace).

    Assemblies

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

    Classes and Structures

    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 Classes

    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.

    Interfaces

    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.

    Functions

    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 Variables

    Pascal Case, no underscores. Try to avoid abbreviations.

    Parameters and Procedure-level Variables

    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 Variables

    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 Forms

    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)

    This is in effect Camel Case; furthermore, this naming convention is known as Hungarian naming convention and is typically discouraged in modern programming. Variables in this style have names like lblSurname or tbSurname, rather than surnameLabel or surnameTextBox. This has the added advantage that similar UI elements are listed alphabetically contiguously in IntelliSense.

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

    Control Prefix Example
    Label lbl lblSurname
    TextBox(ASP.Net) tb tbSurname
    TextBox(WinForms) txt txtUserName
    DataGrid dg dgResults
    GridView gv gvResults2
    Button btn btnSave
    ImageButton iBtn iBtnSave
    Hyperlink lnk lnkHomePage
    ComboBox cmb cmbYear
    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 rBtnGender
    RadioButtonList rBtn rBtnAgeGroup
    Image img imgLogo
    Panel pnl pnlSevtion
    PlaceHolder plh plhHeader
    Calendar cal calMyDate
    AdRotator adr adrBanner
    Table tbl tblResults
    [All] Validators val (N/A) valCreditCardNumber
    ValidationSummary vals (N/A) valsErrors

    Constants

    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.

    Example

    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
            }
        }
    }