C Sharp 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.

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.

Namespace
Namespaces are named using Pascal Case (also called ) with no underscores. This means the first letter of every word in the name is capitalized. For example:. 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, , or. 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 base class, and not inherit from the.

Interfaces
Follow class naming conventions, but start the name with and capitalize the letter following the. Example: The  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 ). 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 or  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 as the prefix followed by a descriptive name such as  or  ("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:

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.

C 샤프 프로그래밍/명명 규칙