< C Sharp Programming

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 discouraged in modern programming. I can't believe it is still being used, I find it annoying. Instead of lblSurname and tbSurname not surnameLabel and surnameTextBox. This has the added advantage that both objects are listed alphabetically contiguously in intellisense.

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

ControlPrefixExample
LabellbllblSurname
TextBox(ASP.Net)tbtbSurname
TextBox(WinForms) txt txtUserName
DataGriddgdgResults
GridViewgvgvResults2
ButtonbtnbtnSave
ImageButtoniBtniBtnSave
HyperlinklnklnkHomePage
ComboBox cmb cmbYear
DropDownListddlddlCompany
ListBoxlstlstCompany
DataListdLstdLstAddress
DataSetdsdsInvoices
DataTabledtdtClients
DataRowdrdrUser
RepeaterreprepSection
CheckboxchkchkMailList
CheckBoxListchkchkAddress
RadioButtonrBtnrBtnGender
RadioButtonListrBtnrBtnAgeGroup
ImageimgimgLogo
PanelpnlpnlSevtion
PlaceHolderplhplhHeader
CalendarcalcalMyDate
AdRotatoradradrBanner
TabletbltblResults
[All] Validatorsval (N/A)valCreditCardNumber
ValidationSummaryvals (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
        }
    }
}


This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.