C# Coding Standards - and Naming Conventions
Here are our C# coding standards, naming conventions, and best practices.
Use these in your own projects and/or adjust these to your own needs. |
1. Naming Conventions and Style
public class ClientActivity
{
public void ClearStatistics()
{
//...
}
public void CalculateStatistics()
{
//...
}
}
public class UserLog
{
public void Add(LogEvent logEvent)
{
int itemCount = logEvent.Items.Count;
// ...
}
}
// Correct
int counter;
string name;
// Avoid
int iCounter;
string strName;
// Correct
public static const string ShippingType = "DropShip";
// Avoid
public static const string SHIPPINGTYPE = "DropShip";
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;
HtmlHelper htmlHelper;
FtpTransfer ftpTranfer;
UIControl uiControl;
with an underscore.
// Correct
public DateTime clientAppointment;
public TimeSpan timeLeft;
// Avoid
public DateTime client_Appointment;
public TimeSpan time_Left;
// Exception
private DateTime _registrationDate;
// Correct
string firstName;
int lastIndex;
bool isSaved;
// Avoid
String firstName;
Int32 lastIndex;
Boolean isSaved;
double, etc) use predefined names.
var stream = File.Create(path);
var customers = new Dictionary<int?, Customer>();
// Exceptions
int index = 100;
string timeSheet;
bool isCompleted;
public class Employee
{
}
public class BusinessLocation
{
}
public class DocumentCollection
{
}
public interface IShape
{
}
public interface IShapeCollection
{
}
public interface IGroupable
{
}
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
{
//...
}
// Examples
namespace Company.Product.Module.SubModule
namespace Product.Module.Component
namespace Product.Layer.Module.Group
// Correct
class Program
{
static void Main(string[] args)
{
}
}
// 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()
{
// ...
}
}
// 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
}
// Don't
public enum Direction : long
{
North = 1,
East = 2,
South = 3,
West = 4
}
// Correct
public enum Direction
{
North,
East,
South,
West
}
// Don't
public enum CoinEnum
{
Penny,
Nickel,
Dime,
Quarter,
Dollar
}
// Correct
public enum Coin
{
Penny,
Nickel,
Dime,
Quarter,
Dollar
}
Note: Over time, we will add sections on Comments, Events, Exceptions, and more...
0 comments:
Post a Comment
Your comments: