Use US-English

Naming identifiers.

  • In Pascal casing the first letter of each word in an identifier is capitalized. For example, BackgroundColor.
  • In Camel casing only the first letter of the second, third, etc. word in a name is capitalized; for example, backgroundColor.
  • In Upper Snake casing each word is capitalized and separated by an underscore, for example, BACKGROUND_COLOR.

The table below provides the casing for the most common types.

IDENTIFIER CASE EXAMPLE

IDENTIFIERCASEEXAMPLE
Class, StructPascalOrderManager
InterfacePascalIOrderRepository
Enum typePascalErrorLevel
Enum valuesPascalFatalError
EventPascalValueChange
Exception classPascalWebException
FieldcamellistOfItems
Const FieldPascalMaximumItems
Read-onlyPascalApiPortNumber
MethodPascalToString
NamespacePascalSystem.Drawing
ParametercameltypeName
PropertyPascalBackgroundColor
VariableCamelexistingOrder
Magic NumbersUpper Snake CaseCOMPANY_GREEN

Do not prefix member fields.

Member fields should not have the type in the name: stringErrorMessage.

Use identitying suffix in non-web GUI elements.

In the GUI design phase when we do not use the MVC framework it is necessary to identify groups of elements with similar names. This is allowed by using a suffix to identify the particular element. Example: FirstNameTextbox, FirstNameLabel.

Do not use casing to differentiate identifiers.

This rule applies to namespaces, properties, methods, method parameters, and types. Please note that it is allowed to have identifiers that differ only in case in distinct categories, e.g. a property BackColor that wraps the field backColor.

Use abbreviations with care.

Do not contract words in identifiers, but do use well-known abbreviations. For example, do not use GetWin instead of GetWindow, but do use a well-known abbreviation such as UI instead of UserInterface.

Name an identifier according to its meaning and not its type.

Avoid using language specific terminology in names of identifiers. As an example, suppose you have a number of overloaded methods to write data types into a stream. Do not use definitions like:

	void Write(double doubleValue);
	void Write(long longValue);

Instead, use:

	void Write(double value);
	void Write(long value);

If it is absolutely required to have a uniquely named method for every data type, use Universal Type Names in the method names. The table below provides the mapping from C# types to Universal types.

NAME

C# TYPE NAMEUNIVERSAL TYPE NAME
sbyteSByte
byteByte
shortInt16
ushortUInt16
intInt32
uintUInt32
longInt64
ulongUInt64
floatSingle
doubleDouble
boolBoolean
charChar
stringString
objectObject

Based on the example above, the corresponding reading methods may look like this:

	double ReadDouble();
	long ReadInt64();

Name namespaces according to a well-defined pattern.

Namespaces should be written in Pascal casing and named according to the following pattern:

   <company>.<technology>.<top-level component>.<bottom-level component>

If existing projects/solutions contain or are composed of non-compliant Namespaces, consider leaving them as is. Exceptions might include those that are in the development process and have yet to reach the Validation phase.

Do not add a suffix to a class or struct name.

Do not add suffixes like Struct or Class to the name of a class or struct.

Example:

	public class OrderClass() // Bad

	public class Order() // Good

Exceptions:

Use a noun or a noun phrase to name a class or struct.

Classes and Structs represent objects. It makes sense that the names should be objects: Order, Person, or Log.

Use a verb or verb phrase for events

Events are raised when something happens and it makes sense to name it as such: “OnClick”

Prefix interfaces with the letter I.

All interfaces should be prefixed with the letter I. Use a noun (e.g. IComponent), noun phrase (e.g. ICustomAttributeProvider), or an adjective (e.g. IPersistable) to name an interface.

Use similar names for the default implementation of an interface.

If you provide a default implementation for a particular interface, use a similar name for the implementing class. Notice that this only applies to classes that only implement that interface.

Example:
A class implementing the IComponent interface should be called Component.

Suffix names of attributes with Attribute.

Although this is not required by the C# compiler, this convention is followed by all built-in attributes.

Some frameworks like ASP.NET MVC or ASP.NET Core require that the attribute names end as described.

Example:
public class AddActivityAttribute

This class would be used by adding the [AddActivity] decorator to a controller action.

Use singular names for enumeration types.

For example, do not name an enumeration type Protocols but name it Protocol instead. Consider the following example in which only one option is allowed.

  public enum Protocol
  {
    Tcp,
    Udp,
    Http,
    Ftp
  }

Use a plural name for enumerations representing bitfields.

Use a plural name for such enumeration types. The following code snippet is a good example of an enumeration that allows combining multiple options.

  [Flags]
  public Enum SearchOptions
  {
  CaseInsensitive = 0x01,
  WholeWordOnly = 0x02,
  AllDocuments = 0x04,
  Backwards = 0x08,
  AllowWildcards = 0x10
  }

Do not use letters that can be mistaken for digits, and vice versa.

To create obfuscated code, use very short, meaningless names formed from the letters O, o, l, I and the digits 0 and 1. Anyone reading code like

   bool b001 = (lo == l0) ? (I1 == 11) : (lOl != 101);

will marvel at your creativity.

Add EventHandler to delegates related to events.

Delegates that are used to define an event handler for an event must be suffixed with EventHandler. For example, the following declaration is correct for a Close event.

public delegate CloseEventHandler(object sender, EventArgs arguments)

Add Callback to delegates related to callback methods.

Delegates that are used to pass a reference to a callback method (so not an event) must be suffixed with “Callback.”

Example:

public delegate AsyncIOFinishedCallback(IpcClient client, string message);

Do not add an Event suffix (or any other type-related suffix) to the name of an event.

  OnClickEvent // Bad
  OnClick // Good

Use an –ing and –ed form to express pre-events and postevents.

If you want to provide distinct events for expressing a point of time before and a point of time after a certain occurrence such as a validation event, use the pattern using xxxing and xxxed (Validating and Validated ), do not use a pattern like BeforeValidation and AfterValidation.

Example:

	public void OnActionExecuting(ActionExecutingContext context)
	public void OnActionExecuted(ActionExecutedContext context)

Prefix an event handler with On.

It is good practice to prefix the method that is registered as an event handler with On. For example, a method that handles the Closing event should be named OnClosing().

Exception:
In some situations, you might be faced with multiple classes exposing the same event name. To allow separate event handlers use a more intuitive name for the event handler, as long as it is prefixed with On.

Name DLL assemblies after their containing namespace.

To allow storing assemblies in the GAC, their names must be unique. Therefore, use the namespace name as a prefix of the name of the assembly. As an example, consider a group of classes organized under the namespace AgapeWORKS.Common. In that case, the assembly generated from those classes will be called AgapeWORKS.Common.dll.

If multiple assemblies are built from the same namespace, it is allowed to append a unique postfix to the namespace name.

Use Pascal casing for naming source files.

Do not use the underscore character and do not use casing to differentiate names of files.

Name the source file to the main class

In addition, do not put more than one major class plus its auxiliary classes (such as EventArgs-derived classes) in one sourcefile.

Use Pascal casing for naming the file and don’t use underscores.

Name a source file to the logical function of the partial type

When using partial types and allocating a part per file, name each file after the logical part that part plays. For example:

// In MyClass.cs
public partial class MyClass
{...}
// In MyClass.Designer.cs
public partial class MyClass
{...}

Limit the contents of a source code file to one class

Exception Nested types should, for obvious reasons, be part of the same file.

Only use the this. construction to avoid a name clash

Do not use the “this.” construction to dereference members. The use of “this.” is only allowed as a reference to the current class instance or to prevent name clashing between method parameters and class fields

Next Section