Coding Guidelines for C# v10 Cheat Sheet |  |
Design & Maintainability (level 1 and 2 only) |
Class Design
|
**Miscellaneous Design**
* Throw exceptions rather than returning some kind of status value (AV1200)
* Provide a rich and meaningful exception message text (AV1202)
* Don't swallow errors by catching generic exceptions (AV1210)
* Properly handle exceptions in asynchronous code (AV1215)
* Always check an event handler delegate for `null` (AV1220)
* Use a protected virtual method to raise each event (AV1225)
* Don't pass `null` as the `sender` argument when raising an event (AV1235)
* Use generic constraints if applicable (AV1240)
* Evaluate the result of a LINQ expression before returning it (AV1250)
* Do not use `this` and `base` prefixes unless it is required (AV1251)
**Maintainability** * Methods should not exceed 7 statements (AV1500) * Make all members `private` and types `internal sealed` by default (AV1501) * Avoid conditions with double negatives (AV1502) * Don't use "magic" numbers (AV1515) * Only use `var` when the type is very obvious (AV1520) * Declare and initialize variables as late as possible (AV1521) * Assign each variable in a separate statement (AV1522) * Favor object and collection initializers over separate statements (AV1523) * Don't make explicit comparisons to `true` or `false` (AV1525) * Don't change a loop variable inside a `for` loop (AV1530) * Avoid nested loops (AV1532) |
* Always add a block after the keywords `if`, `else`, `do`, `while`, `for`, `foreach` and `case` (AV1535)
* Always add a `default` block after the last `case` in a `switch` statement (AV1536)
* Finish every `if`-`else`-`if` statement with an `else` clause (AV1537)
* Be reluctant with multiple `return` statements (AV1540)
* Don't use an `if`-`else` construct instead of a simple (conditional) assignment (AV1545)
* Encapsulate complex expressions in a property, method or local function (AV1547)
* Call the more overloaded method from other overloads (AV1551)
* Only use optional parameters to replace overloads (AV1553)
* Do not use optional parameters in interface methods or their concrete implementations (AV1554)
* Avoid using named arguments (AV1555)
* Don't declare signatures with more than 3 parameters (AV1561)
* Don't use `ref` or `out` parameters (AV1562)
* Avoid signatures that take a `bool` flag (AV1564)
* Prefer `is` patterns over `as` operations (AV1570)
* Don't comment out code (AV1575)
* Write code that is easy to debug (AV1580)
**Framework Guidelines** * Use C# type aliases instead of the types from the `System` namespace (AV2201) * Prefer language syntax over explicit calls to underlying implementations (AV2202) * Build with the highest warning level (AV2210) * Use lambda expressions instead of anonymous methods (AV2221) * Only use the `dynamic` keyword when talking to a dynamic object (AV2230) * Favor `async`/`await` over `Task` continuations (AV2235) |
||||||||
Dennis Doomen Version %semver% (%commitdate%) | [www.csharpcodingguidelines.com](http://www.csharpcodingguidelines.com) [www.continuousimprover.com](www.continuousimprover.com) [www.avivasolutions.nl](http://www.avivasolutions.nl) |
Coding Guidelines for C# v10 Cheat Sheet |
| ||
Naming & Layout (level 1 and 2 only) |
|
* Use an underscore for irrelevant parameters (AV1739)
**Documentation**
* Write comments and documentation in US English (AV2301)
* Document all `public`, `protected` and `internal` types and members (AV2305)
* Write XML documentation with other developers in mind (AV2306)
* Avoid inline comments (AV2310)
* Only write comments to explain complex algorithms or decisions (AV2316)
**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` (AV1535) * 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 * Remove redundant parentheses in expressions if they do not clarify precedence; add parentheses in expressions to avoid non-obvious precedence * Do not use `#region` (AV2407) * Use expression-bodied members appropriately (AV2410) |
||
Dennis Doomen Version %semver% (%commitdate%) | [www.csharpcodingguidelines.com](http://www.csharpcodingguidelines.com) [www.continuousimprover.com](www.continuousimprover.com) [www.avivasolutions.nl](http://www.avivasolutions.nl) |