top of page

Arcadia C# Coding Standards and Naming Conventions

Below are our C# coding standards, naming conventions, and best practices. Use these in your own projects and/or adjust these to your own needs.

Do use PascalCasing for class names and method names.

  1. public class ClientActivity

  2. {

  3. public void ClearStatistics()

  4. {

  5. //...

  6. }

  7. public void CalculateStatistics()

  8. {

  9. //...

  10. }

  11. }

Why: consistent with the Microsoft's .NET Framework and easy to read.

Do use camelCasing for method arguments and local variables.

  1. public class UserLog

  2. {

  3. public void Add(LogEvent logEvent)

  4. {

  5. int itemCount = logEvent.Items.Count;

  6. // ...

  7. }

  8. }

Why: consistent with the Microsoft's .NET Framework and easy to read.

Do not use Hungarian notation or any other type identification in identifiers

  1. // Correct

  2. int counter;

  3. string name;

  4. // Avoid

  5. int iCounter;

  6. string strName;

Why: consistent with the Microsoft's .NET Framework and Visual Studio IDE makes determining types very easy (via tooltips). In general you want to avoid type indicators in any identifier.

Do not use Screaming Caps for constants or readonly variables

  1. // Correct

  2. public static const string ShippingType = "DropShip";

  3. // Avoid

  4. public static const string SHIPPINGTYPE = "DropShip";

Why: consistent with the Microsoft's .NET Framework. Caps grap too much attention.

Avoid using Abbreviations. Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri

  1. // Correct

  2. UserGroup userGroup;

  3. Assignment employeeAssignment;

  4. // Avoid

  5. UserGroup usrGrp;

  6. Assignment empAssignment;

  7. // Exceptions

  8. CustomerId customerId;

  9. XmlDocument xmlDocument;

  10. FtpHelper ftpHelper;

  11. UriPart uriPart;

Why: consistent with the Microsoft's .NET Framework and prevents inconsistent abbreviations.

Do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase)

  1. HtmlHelper htmlHelper;

  2. FtpTransfer ftpTransfer;

  3. UIControl uiControl;

Why: consistent with the Microsoft's .NET Framework. Caps would grap visually too much attention.

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

  1. // Correct

  2. public DateTime clientAppointment;

  3. public TimeSpan timeLeft;

  4. // Avoid

  5. public DateTime client_Appointment;

  6. public TimeSpan time_Left;

  7. // Exception

  8. private DateTime _registrationDate;

Why: consistent with the Microsoft's .NET Framework and makes code more natural to read (without 'slur'). Also avoids underline stress (inability to see underline).

Do use predefined type names instead of system type names like Int16, Single, UInt64, etc

  1. // Correct

  2. string firstName;

  3. int lastIndex;

  4. bool isSaved;

  5. // Avoid

  6. String firstName;

  7. Int32 lastIndex;

  8. Boolean isSaved;

Why: consistent with the Microsoft's .NET Framework and makes code more natural to read.

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

  1. var stream = File.Create(path);

  2. var customers = new Dictionary();

  3. // Exceptions

  4. int index = 100;

  5. string timeSheet;

  6. bool isCompleted;

Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.

Do use noun or noun phrases to name a class.

  1. public class Employee

  2. {

  3. }

  4. public class BusinessLocation

  5. {

  6. }

  7. public class DocumentCollection

  8. {

  9. }

Why: consistent with the Microsoft's .NET Framework and easy to remember.

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

  1. public interface IShape

  2. {

  3. }

  4. public interface IShapeCollection

  5. {

  6. }

  7. public interface IGroupable

  8. {

  9. }

Why: consistent with the Microsoft's .NET Framework.

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.

  1. // Located in Task.cs

  2. public partial class Task

  3. {

  4. //...

  5. }

  6. // Located in Task.generated.cs

  7. public partial class Task

  8. {

  9. //...

  10. }

Why: consistent with the Microsoft practices. Files are alphabetically sorted and partial classes remain adjacent.

Do organize namespaces with a clearly defined structure

  1. // Examples

  2. namespace Company.Product.Module.SubModule

  3. namespace Product.Module.Component

  4. namespace Product.Layer.Module.Group

Why: consistent with the Microsoft's .NET Framework. Maintains good organization of your code base.

Do vertically align curly brackets.

  1. // Correct

  2. class Program

  3. {

  4. static void Main(string[] args)

  5. {

  6. }

  7. }

Why: Microsoft has a different standard, but developers have overwhelmingly preferred vertically aligned brackets.

Do declare all member variables at the top of a class, with static variables at the very top.

  1. // Correct

  2. public class Account

  3. {

  4. public static string BankName;

  5. public static decimal Reserves;

  6. public string Number {get; set;}

  7. public DateTime DateOpened {get; set;}

  8. public DateTime DateClosed {get; set;}

  9. public decimal Balance {get; set;}

  10. // Constructor

  11. public Account()

  12. {

  13. // ...

  14. }

  15. }

Why: generally accepted practice that prevents the need to hunt for variable declarations.

Do use singular names for enums. Exception: bit field enums.

  1. // Correct

  2. public enum Color

  3. {

  4. Red,

  5. Green,

  6. Blue,

  7. Yellow,

  8. Magenta,

  9. Cyan

  10. }

  11. // Exception

  12. [Flags]

  13. public enum Dockings

  14. {

  15. None = 0,

  16. Top = 1,

  17. Right = 2,

  18. Bottom = 4,

  19. Left = 8

  20. }

Why: consistent with the Microsoft's .NET Framework and makes the code more natural to read. Plural flags because enum can hold multiple values (using bitwise 'OR').

Do not explicitly specify a type of an enum or values of enums (except bit fields)

  1. // Don't

  2. public enum Direction : long

  3. {

  4. North = 1,

  5. East = 2,

  6. South = 3,

  7. West = 4

  8. }

  9. // Correct

  10. public enum Direction

  11. {

  12. North,

  13. East,

  14. South,

  15. West

  16. }

Why: can create confusion when relying on actual types and values.

Do not suffix enum names with Enum

  1. // Don't

  2. public enum CoinEnum

  3. {

  4. Penny,

  5. Nickel,

  6. Dime,

  7. Quarter,

  8. Dollar

  9. }

  10. // Correct

  11. public enum Coin

  12. {

  13. Penny,

  14. Nickel,

  15. Dime,

  16. Quarter,

  17. Dollar

  18. }

Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.

Do suffix abstract class with naming “Base”. Abstract names are noun (phrases) or adjectives.

  1. public abstract class ShapeBase

  2. {

  3. }

  4. public abstract class ShapeCollectionBase

  5. {

  6. }

  7. public abstract class GroupableBase

  8. {

  9. }

Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page