C# Coding Standards - YichenGuan6/BattleShip GitHub Wiki

C# Coding Standards

Object Name Notation
Class name PascalCase
Constructor name PascalCase
Method name PascalCase
Method arguments camelCase
Local variables camelCase
Constants name PascalCase
Field name camelCase
Properties name PascalCase
Enum type name PascalCase

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

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

2. Do use camelCasing for method arguments and local variables:

public class UserLog
{
  public void Add(LogEvent logEvent)
  {
    int itemCount = logEvent.Items.Count;
    // ...
  }
}

3. Use meaningful names for variables. The following example uses seattleCustomers for customers who are located in Seattle:

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

4. Do not use Underscores in identifiers. Exception: you can prefix private fields 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;

5. Do use predefined type names (C# aliases) like int, float, string for local, parameter and member declarations.

// Correct
string firstName;
int lastIndex;
bool isSaved;
// Avoid
String firstName;
Int32 lastIndex;
Boolean isSaved;

6. Do use noun or noun phrases to name a class.

public class Employee
{
}
public class BusinessLocation
{
}
public class DocumentCollection
{
}

7. Do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.

public interface IShape
{
}
public interface IShapeCollection
{
}
public interface IGroupable
{
}

8. Do vertically align curly brackets:

// Correct
class Program
{
  static void Main(string[] args)
  {
    //...
  }
}

9. 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()
  {
    // ...
  }
}

10. 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
}

11. 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
}

12. Do not use an "Enum" suffix in enum type names:

// Don't
public enum CoinEnum
{
  Penny,
  Nickel,
  Dime,
  Quarter,
  Dollar
} 
// Correct
public enum Coin
{
  Penny,
  Nickel,
  Dime,
  Quarter,
  Dollar
}

13. Do not use "Flag" or "Flags" suffixes in enum type names:

// Don't
[Flags]
public enum DockingsFlags
{
  None = 0,
  Top = 1,
  Right = 2, 
  Bottom = 4,
  Left = 8
}
// Correct
[Flags]
public enum Dockings
{
  None = 0,
  Top = 1,
  Right = 2, 
  Bottom = 4,
  Left = 8
}

14. Do not create names of parametres in methods (or constructors) which differ only by the register:

// Avoid
private void MyFunction(string name, string Name)
{
  //...
}

Offical Reference

  1. MSDN General Naming Conventions
  2. DoFactory C# Coding Standards and Naming Conventions
  3. MSDN Naming Guidelines
  4. MSDN Framework Design Guidelines