0% found this document useful (0 votes)
270 views

C# Coding Guidelines Cheat Sheet

The document provides coding guidelines for C# 3.0, 4.0 and 5.0 organized into sections on design and maintainability, class design, member design, naming and layout, documentation, and framework guidelines. It includes over 80 guidelines on topics like exception handling, LINQ usage, method and parameter design, commenting best practices, and more. The full document is intended to help developers write clean, readable, maintainable C# code.

Uploaded by

Tarass Demchuk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
270 views

C# Coding Guidelines Cheat Sheet

The document provides coding guidelines for C# 3.0, 4.0 and 5.0 organized into sections on design and maintainability, class design, member design, naming and layout, documentation, and framework guidelines. It includes over 80 guidelines on topics like exception handling, LINQ usage, method and parameter design, commenting best practices, and more. The full document is intended to help developers write clean, readable, maintainable C# code.

Uploaded by

Tarass Demchuk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Coding Guidelines for C# 3.0, 4.0 and 5.

0
Cheat Sheet
Design & Maintainability (level 1 and 2 only)
Basic Principles Miscellaneous Design • Always add a block after keywords such if , else , while , for ,
• The Principle of Least Surprise • Throw exceptions rather than returning status values (AV1200) foreach and case (AV1535)
• Keep It Simple Stupid • Always add a default block after the last case in a switch
• You Ain’t Gonne Need It • Provide a rich and meaningful exception message text (AV1202)
• Don’t Repeat Yourself • Don’t swallow errors by catching generic exceptions (AV1210) statement (AV1536)
• Always check an event handler delegate for null (AV1220) • Finish every if ­ else ­ if statement with an else ­part (AV1537)
Class Design • Be reluctant with multiple return statements (AV1540)
• Properly handle exceptions in asynchronous code (AV1215)
• A class or interface should have a single purpose (AV1000) • Use a protected virtual method to raise each event (AV1225) • Don’t use if ­ else statements instead of a simple (conditional)
• An interface should be small and focused (AV1003) • Don’t pass null as the sender parameter when raising an event assignment (AV1545)
• Use an interface to decouple classes from each other (AV1005) (AV1235) • Encapsulate complex expressions in a method or property
• Don’t hide inherited members with the new keyword (AV1010) • Use generic constraints if applicable (AV1240) (AV1547)
• It should be possible to treat a derived object as if it were a base • Evaluate the result of a LINQ expression before returning it • Call the most overloaded method from other overloads (AV1551)
class object (AV1011) (AV1250) • Only use optional arguments to replace overloads (AV1553)
• Don’t refer to derived classes from the base class (AV1013) • Avoid using named arguments (AV1555)
• Avoid exposing the objects an object depends on (AV1014) • Don’t allow methods and constructors with more than three
Maintainability
• Avoid bidirectional dependencies (AV1020) parameters (AV1561)
• Methods should not exceed 7 statements (AV1500)
• Classes should have state and behavior (AV1025) • Don’t use ref or out parameters (AV1562)
• Make all members private and types internal by default
• Avoid methods that take a bool flag (AV1564)
(AV1501)
Member Design • Always check the result of an as operation (AV1570)
• Avoid conditions with double negatives (AV1502)
• Allow properties to be set in any order (AV1100) • Don’t comment­out code (AV1575)
• Don’t use "magic numbers" (AV1515)
• Don’t use mutual exclusive properties (AV1110) • Only use var when the type is very obvious (AV1520)
• A method or property should do only one thing (AV1115) Framework Guidelines
• Declare and initialize variables as late as possible (AV1521)
• Don’t expose stateful objects through static members (AV1125) • Favor Object and Collection Initializers over separate statements • Use C# type aliases instead of the types from the System
• Return an IEnumerable<T> or ICollection<T> instead of a (AV1523) namespace (AV2201)
concrete collection class (AV1130) • Don’t make explicit comparisons to true or false (AV1525) • Build with the highest warning level (AV2210)
• Properties, methods and arguments representing strings or • Don’t change a loop variable inside a for or foreach loop • Use Lambda expressions instead of delegates (AV2221)
collections should never be null (AV1135) (AV1530) • Only use the dynamic keyword when talking to a dynamic object
• Define parameters as specific as possible (AV1137) • Avoid nested loops (AV1532) (AV2230)
• Favor async / await over the Task (AV2235)
www.csharpcodingguidelines.com
Dennis Doomen www.continuousimprover.com
Version 5.0.0 (October 18, 2016) www.avivasolutions.nl
Coding Guidelines for C# 3.0, 4.0 and 5.0
Cheat Sheet
Naming & Layout (level 1 and 2 only)
Pascal Casing • Use an underscore for irrelevant lambda parameters (AV1739) Empty lines
Class, Struct AppDomain Documentation • Between members
Interface IBusinessService • After the closing parentheses
Enumeration type ErrorLevel • Write comments and documentation in US English (AV2301) • Between multi­line statements
Enumeratiion values FatalError • Document all public, protected and internal types and members • Between unrelated code blocks
Event Click • Around the #region keyword
(AV2305) • Between the using statements of different root namespaces.
Protected field MainPanel
Const field MaximumItems • Avoid inline comments (AV2310)
Read­only static field RedValue • Only write comments to explain complex algorithms or decisions Member order
Method ToString
(AV2316) 1. Private fields and constants
Namespace System.Drawing 2. Public constants
Property BackColor • Don’t use comments for tracking work to be done later (AV2318) 3. Public read­only static fields
Type Parameter TEntity Layout 4. Factory Methods
5. Constructors and the Finalizer
Camel Casing • Maximum line length is 130 characters. 6. Events
Private field listItem 7. Public Properties
• Indent 4 spaces, don’t use Tabs 8. Other methods and private properties in calling order
Variable listOfValues
• Keep one white­space between keywords like if and the
Const variable maximumItems
Parameter typeName expression, but don’t add white­spaces after ( and before ) .
Important Note
• Add a white­space around operators, like + , ­ , == , etc. These coding guidelines are an extension to Visual Studio's Code
• Always add parentheses after keywords if , else , do , while , Analysis functionalty, so make sure you enable that for all your
Naming projects. Check the full document for more details.
for and foreach
• Use US English (AV1701) • Always put opening and closing parentheses on a new line.
• Don’t include numbers in variables, parameters and type • Don’t indent object initializers and initialize each property on a
members (AV1704) new line.
• Don’t prefix fields (AV1705) • Don’t indent lambda statements
• Don’t use abbreviations (AV1706) • Put the entire LINQ statement on one line, or start each keyword
• Name members, parameters or variables according its meaning at the same indentation.
and not its type (AV1707) • Add braces around comparison conditions, but don’t add braces
• Name types using nouns, noun phrases or adjective phrases around a singular condition.
(AV1708)
• Don’t repeat the name of a class or enumeration in its members
(AV1710)
• Avoid short names or names that can be mistaken with other
names (AV1712)
• Name methods using verb­object pair (AV1720)
• Name namespaces using names, layers, verbs and features
(AV1725)

Dennis Doomen www.csharpcodingguidelines.com


Version 5.0.0 (October 18, 2016) www.continuousimprover.com
www.avivasolutions.nl

You might also like