Skip to content

Instantly share code, notes, and snippets.

@SangSuantak
Last active April 27, 2017 07:12
Show Gist options
  • Save SangSuantak/00dca35bd92dde46a3a0 to your computer and use it in GitHub Desktop.
Save SangSuantak/00dca35bd92dde46a3a0 to your computer and use it in GitHub Desktop.
C# Coding Standards/Conventions
// References
// 1. http://www.dofactory.com/reference/csharp-coding-standards
// 2. Modifications as per our usage
// 1. do use PascalCasing for class names and method names.
// --------------------------------------------------------
public class ClientActivity
{
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.do not use Hungarian notation or any other type identification in identifiers
// -------------------------------------------------------------------------------
// Correct
int counter;
string name;
// Avoid
int iCounter;
string strName;
// 4.do not use Screaming Caps for constants or readonly variables
// ---------------------------------------------------------------
// Correct
public static const string ShippingType = "DropShip";
// Avoid
public static const string SHIPPINGTYPE = "DropShip";
// 5. 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;
// 6. do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase)
// ------------------------------------------------------------------------------------------
HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;
// 7. do use Underscore prefix for private variables
// 8. 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;
// 9. 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;
// 10. do use noun or noun phrases to name a class.
// ------------------------------------------------
public class Employee
{
}
public class BusinessLocation
{
}
public class DocumentCollection
{
}
// 11. do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
// ---------------------------------------------------------------------------------------------
public interface IShape
{
}
public interface IShapeCollection
{
}
public interface IGroupable
{
}
// 12. do organize namespaces with a clearly defined structure
// -----------------------------------------------------------
// Examples
namespace Company.Product.Module.SubModule
namespace Product.Module.Component
namespace Product.Layer.Module.Group
// 13. do vertically align curly brackets.
// ---------------------------------------
// Correct
class Program
{
static void Main(string[] args)
{
}
}
// 15. 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()
{
// ...
}
}
// 16. 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
}
// 17. 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
}
// 18. 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment