Coding Standards
Below are our C# coding standards, naming conventions, and best practices. These standards are adhere to Microsoft's Framework Design Guideline and Coding Conventions.
This guideline is not exhaustive. In cases that are not covered by these standards, follow the principles of Microsoft's Framework Design Guideline and Coding Conventions.
Class Names
✅Do use PascalCasing for class names and method names.
public class ClientActivity
{
public void ClearStatistics()
{
//...
}
public void CalculateStatistics()
{
//...
}
}
Variable Names
✅Do use camelCasing for local variables and method arguments.
public class UserLog
{
public void Add(LogEvent logEvent)
{
int itemCount = logEvent.Items.Count;
// ...
}
}
Identifiers
⛔Do not use Hungarian notation or any other type identification in identifiers
// Correct
int counter;
string name;
// Avoid
int iCounter;
string strName;
Constants
⛔Do not use Caps for constants or readonly variables
// Correct
public static const string ShippingType = "DropShip";
// Avoid
public static const string SHIPPINGTYPE = "DropShip";
Abbreviations
⛔Do not use 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;
Abbreviation Casing
✅Do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase)
HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;
No Underscores
⛔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
private DateTime _registrationDate;
Type Names
✅Do use predefined type names instead of system type names like Int16, Single, UInt64, etc
// Correct
string firstName;
int lastIndex;
bool isSaved;
// Avoid
String firstName;
Int32 lastIndex;
Boolean isSaved;
Implicit Types
✅Do use implicit type var for local variable declarations. Exception: primitive types (int, string, double, etc) use predefined names.
var stream = File.Create(path);
var customers = new Dictionary();
// Exceptions
int index = 100;
string timeSheet;
bool isCompleted;
Noun Class Names
✅Do use noun or noun phrases to name a class.
public class Employee
{
}
public class BusinessLocation
{
}
public class DocumentCollection
{
}
Interfaces
✅Do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
public interface IShape
{
}
public interface IShapeCollection
{
}
public interface IGroupable
{
}
File Names
✅Do name source files according to their main classes.
Exception: file names with partial classes reflect their source or purpose, e.g. designer, generated, etc.
// Located in Task.cs
public partial class Task
{
//...
}
// Located in Task.generated.cs
public partial class Task
{
//...
}
Namespaces
✅Do organize namespaces with a clearly defined structure
// Examples
namespace Company.Product.Module.SubModule
namespace Product.Module.Component
namespace Product.Layer.Module.Group
Curly Brackets
✅Do vertically align curly brackets.
// Correct
class Program
{
static void Main(string[] args)
{
}
}
Member Variables
✅Do declare all member variables at the top of a class, with static variables at the very top.
// Correct
public class Account
{
public static string BankName;
public static decimal Reserves;
public string Number {get; set;}
public DateTime DateOpened {get; set;}
public DateTime DateClosed {get; set;}
public decimal Balance {get; set;}
// Constructor
public Account()
{
// ...
}
}
Enums
✅Do use singular names for enums.
Exception: bit field 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
}
Enum Types
⛔Do not explicitly specify a type of an enum or values of enums (except bit fields)
// Don't
public enum Direction : long
{
North = 1,
East = 2,
South = 3,
West = 4
}
// Correct
public enum Direction
{
North,
East,
South,
West
}
Enum Suffix
⛔Do not suffix enum names with Enum
// Don't
public enum CoinEnum
{
Penny,
Nickel,
Dime,
Quarter,
Dollar
}
// Correct
public enum Coin
{
Penny,
Nickel,
Dime,
Quarter,
Dollar
}