Edit this page

Coding Guidelines for C# 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 7.1, 7.2 and 7.3 Cheat Sheet ![logo](assets/images/logo.png)
Design & Maintainability
(level 1 and 2 only)

Class Design

  • A class or interface should have a single purpose (RG1000)
  • An interface should be small and focused (RG1003)
  • Use an interface to decouple classes from each other (RG1005)
  • Don’t suppress compiler warnings using the new keyword (RG1010)
  • It should be possible to treat a derived object as if it were a base class object (RG1011)
  • Don’t refer to derived classes from the base class (RG1013)
  • Avoid exposing the other objects an object depends on (RG1014)
  • Avoid bidirectional dependencies (RG1020)
  • Classes should have state and behavior (RG1025)
  • Classes should protect the consistency of their internal state (RG1026)


Member Design

  • Allow properties to be set in any order (RG1100)
  • Don’t use mutually exclusive properties (RG1110)
  • A property, method or local function should do only one thing (RG1115)
  • Don’t expose stateful objects through static members (RG1125)
  • Return an IEnumerable<T> or ICollection<T> instead of a concrete collection class (RG1130)
  • Properties, arguments and return values representing strings or collections should never be null (RG1135)
  • Define parameters as specific as possible (RG1137)
**Miscellaneous Design** * Throw exceptions rather than returning some kind of status value (RG1200) * Provide a rich and meaningful exception message text (RG1202) * Don't swallow errors by catching generic exceptions (RG1210) * Properly handle exceptions in asynchronous code (RG1215) * Always check an event handler delegate for `null` (RG1220) * Use a protected virtual method to raise each event (RG1225) * Don't pass `null` as the `sender` argument when raising an event (RG1235) * Use generic constraints if applicable (RG1240) * Evaluate the result of a LINQ expression before returning it (RG1250) * Do not use `this` and `base` prefixes unless it is required (RG1251)
**Maintainability** * Methods should not exceed 7 statements (RG1500) * Make all members `private` and types `internal sealed` by default (RG1501) * Avoid conditions with double negatives (RG1502) * Don't use "magic" numbers (RG1515) * Only use `var` when the type is very obvious (RG1520) * Declare and initialize variables as late as possible (RG1521) * Assign each variable in a separate statement (RG1522) * Favor object and collection initializers over separate statements (RG1523) * Don't make explicit comparisons to `true` or `false` (RG1525) * Don't change a loop variable inside a `for` loop (RG1530) * Avoid nested loops (RG1532)
* Always add a block after the keywords `if`, `else`, `do`, `while`, `for`, `foreach` and `case` (RG1535) * Always add a `default` block after the last `case` in a `switch` statement (RG1536) * Finish every `if`-`else`-`if` statement with an `else` clause (RG1537) * Be reluctant with multiple `return` statements (RG1540) * Don't use an `if`-`else` construct instead of a simple (conditional) assignment (RG1545) * Encapsulate complex expressions in a property, method or local function (RG1547) * Call the more overloaded method from other overloads (RG1551) * Only use optional arguments to replace overloads (RG1553) * Avoid using named arguments (RG1555) * Don't declare signatures with more than 3 parameters (RG1561) * Don't use `ref` or `out` parameters (RG1562) * Avoid signatures that take a `bool` flag (RG1564) * Prefer `is` patterns over `as` operations (RG1570) * Don't comment out code (RG1575)
**Framework Guidelines** * Use C# type aliases instead of the types from the `System` namespace (RG2201) * Prefer language syntax over explicit calls to underlying implementations (RG2202) * Build with the highest warning level (RG2210) * Use lambda expressions instead of anonymous methods (RG2221) * Only use the `dynamic` keyword when talking to a dynamic object (RG2230) * Favor `async`/`await` over `Task` continuations (RG2235)

</table>

Coding Guidelines for C# 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 7.1, 7.2 and 7.3 Cheat Sheet

logo</td> </tr>

Naming & Layout
(level 1 and 2 only)


Naming

  • Use US English (RG1701)
  • Don’t prefix fields (RG1705)
  • Don’t use abbreviations (RG1706)
  • Name members, parameters and variables according to their meaning and not their type (RG1707)
  • Name types using nouns, noun phrases or adjective phrases (RG1708)
  • Name generic type parameters with descriptive names (RG1709)
  • Don’t repeat the name of a class or enumeration in its members (RG1710)
  • Avoid short names or names that can be mistaken for other names (RG1712)
  • Properly name properties (RG1715)
  • Name methods and local functions using verbs or verb-object pairs (RG1720)
  • Use a verb or verb phrase to name an event (RG1735)
  • Postfix asynchronous methods with Async or TaskAsync (RG1755)
* Use an underscore for irrelevant parameters (RG1739) **Documentation** * Write comments and documentation in US English (RG2301) * Document all `public`, `protected` and `internal` types and members (RG2305) * Write XML documentation with other developers in mind (RG2306) * Avoid inline comments (RG2310) * Only write comments to explain complex algorithms or decisions (RG2316)
**Layout** * Maximum line length is 130 characters * Indent 4 spaces, don't use tabs * Keep one space between keywords like `if` and the expression, but don't add spaces after `(` and before `)` * Add a space around operators, like `+`, `-`, `==`, etc. * Always add curly braces after the keywords `if`, `else`, `do`, `while`, `for`, `foreach` and `case` (RG1535) * Always put opening and closing curly braces on a new line * Don't indent object/collection initializers and initialize each property on a new line * Don't indent lambda statement blocks * Keep expression-bodied-members on one line; break long lines after the arrow sign * Put the entire LINQ statement on one line, or start each keyword at the same indentation * Add parentheses around every binary expression, but don't add parentheses around unary expressions
* Be reluctant with #region (RG2407) * Use expression-bodied members appropriately (RG2410)