C# Coding Standards Best Practices

C# Coding Standards Best Practices

Class Names

Use PascalCasing for class names and method names.

public class MojaKlasa
{    
   public void ObliczCos()    
   {        
      //...    
   }    
   public void ObliczCosInnego()    
   {        
      //...    
   }
}

Variable Names

Use camelCasing for local variables and method arguments.

public class User
{    
   public void AddUser(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 Screaming Caps for constants or readonly variables.

// Correct
public static const string ShippingType = "DropShip"; 

// Avoid
public static const string SHIPPINGTYPE = "DropShip";

Abbreviations

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
SupplierId supplierId;
XmlDocument xmlDocument;
FtpHelper ftpHelper;
UriPart uriPart;

Abbreviation Casing

Use PascalCasing for shortcuts of 3 characters or more (2 characters in uppercase).

HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;

No Underscores

Do notuse Underscores in identifiers.
Exception: you can prefix private static variables with an underscore.

// Correct
public DateTime supplierAppointment;
public TimeSpan timeLeft; 

// Avoid
public DateTime supplier_Appointment;
public TimeSpan time_Left;
 
// Exception
private DateTime _registrationDate;

Type Names

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

Use implicit type var for local variable declarations.
Exception: primitive types (int, string, double, etc) use predefined names.

// Correct
var stream = File.Create(path);
var customers = new Dictionary(); 

// Exceptions
int index = 100;
string timeSheet;
bool isCompleted;

Noun Class Names

Use noun or noun phrases to name a class.

public class Supplier
{}
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 Supplier.cs
public partial class Supplier
{ 
 //...
}

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.

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.

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

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).

// incorrect
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.

// incorrect
public enum CoinEnum
{    
   Penny,    
   Nickel,    
   Dime,    
   Quarter,    
   Dollar
} 

// Correct
public enum Coin
{    
   Penny,    
   Nickel,    
   Dime,    
   Quarter,    
   Dollar
}

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *