Skip to content

Coding Conventions

André "MG" Wisén edited this page Jan 22, 2019 · 3 revisions

Names and Capitalization

Object Name Notation
Class name PascalCase
Method name PascalCase
Method arguments camelCase
Function name PascalCase
Function arguments camelCase
Local variables camelCase

Examples

1. Do use PascalCasing for class names and method names:

public class ClientActivity
{
  public void ClearStatistics()
  {
    //...
  }
  public void CalculateStatistics()
  {
    //...
  }
}

#### 2. Use meaningful names for variables.<br>The following example uses **seattleCustomers** for customers who are located in Seattle:

```csharp
var seattleCustomers = from cust in customers
  where cust.City == "Seattle" 
  select cust.Name;

3. Avoid using Abbreviations.

Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri.

// Correct
UserGroup userGroup;
Assignment employeeAssignment;     
// Avoid
UserGroup usrGrp;
Assignment empAssignment; 
// Exceptions
CustomerId customerId;
XmlDocument xmlDocument;
FtpHelper ftpHelper;
UriPart uriPart;

4. Do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase):

HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;

5. Do not use Underscores in identifiers.

Exception: You can prefix private static variables with an underscore:

// Correct
public DateTime clientAppointment;
public TimeSpan timeLeft;    
// Avoid
public DateTime client_Appointment;
public TimeSpan time_Left; 
// Exception (Class field)
private DateTime _registrationDate;

6. Do use singular names for enums:

// Correct
public enum Color
{
  Red,
  Green,
  Blue,
  Yellow,
  Magenta,
  Cyan
} 
// Exception
[Flags]
public enum Dockings
{
  None = 0,
  Top = 1,
  Right = 2, 
  Bottom = 4,
  Left = 8
}

Credits

Templates for naming convention