Anda di halaman 1dari 2

Coding Guidelines for C# 3.0 and C# 4.

0 Cheat Sheet
Design & Maintainability (level 1 and 2 only)
Miscellaneous Design  Always add a default block after the last case in a switch
Basic Principles
 The Principle of Least Surprise  Throw exceptions rather than returning status values statement (AV1536)
 Keep It Simple Stupid (AV1200)  Finish every if-else-if statement with an else-part
 You Ain’t Gonne Need It  Provide a rich and meaningful exception message text (AV1537)
 Don’t Repeat Yourself (AV1202)  Be reluctant with multiple return statements (AV1540)
 Don’t swallow errors by catching generic exceptions  Don’t use selection statements instead of a simple
(AV1210) assignment or initialization (AV1545)
Class Design  Always check an event handler delegate for null  Prefer conditional statements instead of simple if-else
 A class or interface should have a single purpose (AV1220) constructs (AV1546)
(AV1000)  Use a protected virtual method to raise each event  Encapsulate complex expressions in a method or
 An interface should be small and focused (AV1003) (AV1225) property (AV1547)
 Use an interface to decouple classes from each other  Don’t pass null as the sender parameter when raising  Call the most overloaded method from other overloads
(AV1005) an event (AV1235) (AV1551)
 Don’t hide inherited members with the new keyword  Use generic constraints if applicable (AV1240)  Only use optional parameters to replace overloads
(AV1010)  Don’t add extension methods to the same namespace as (AV1553)
 It should be possible to treat a derived object as if it were the extended class (AV1245)  Avoid using named parameters (AV1555)
a base class object (AV1011)  Evaluate the result of a LINQ expression before returning  Avoid methods with more than three parameters
 Don’t refer to derived classes from the base class it (AV1250) (AV1561)
(AV1013)  Don’t use ref or out parameters (AV1562)
 Avoid exposing the objects an object depends on  Avoid methods that take a bool flag (AV1564)
Maintainability
(AV1014)  Always check the result of an as operation (AV1570)
 Avoid bidirectional dependencies (AV1020)  Methods should not exceed 7 statements (AV1500)
 Don’t comment-out code (AV1575)
 Classes should have state and behavior (AV1025)  Make all members private and types internal by default
 Consider abstracting an external dependency or 3rd party
(AV1501)
component (AV1580)
 Avoid conditions with double negatives (AV1502)
Member Design  Don’t use "magic numbers" (AV1515)
 Allow properties to be set in any order (AV1100)  Only use var when the type is very obvious (AV1520) Framework Guidelines
 Avoid mutual exclusive properties (AV1110)  Initialize variables at the point of declaration (AV1521)  Use C# type aliases instead of the types from the System
 A method or property should do only one thing (AV1115)  Favor Object and Collection Initializers over separate namespace (AV2201)
 Don’t expose stateful objects through static members statements (AV1523)  Build with the highest warning level (AV2210)
(AV1125)  Don’t make explicit comparisons to true or false  Use Lambda expressions instead of delegates (AV2221)
 Return an IEnumerable<T> or ICollection<T> (AV1525)  Only use the dynamic keyword when talking to a dynamic
instead of a concrete collection class (AV1130)  Don’t change a loop variable inside a for or foreach object (AV2230)
 String, list and collection properties should never return a loop (AV1530)
null reference (AV1135)  Don’t use nested loops in a method (AV1532)
 Add a block after all flow control keywords, even if it is
empty (AV1535)

www.csharpcodingguidelines.com
January 2011 www.dennisdoomen.net
Dennis Doomen www.avivasolutions.nl
Coding Guidelines for C# 3.0 and C# 4.0 Cheat Sheet
Naming & Layout (level 1 and 2 only)
 Avoid inline comments (AV2310)
Pascal Casing Member order
Class, Struct AppDomain  Don’t use /* */ for comments (AV2315) 1. Private fields and constants
Interface IBusinessService  Only write comments to explain complex algorithms or 2. Public constants
Enumeration type ErrorLevel decisions (AV2316) 3. Public read-only static fields
Enumeratiion values FatalError  Don’t use comments for tracking work to be done later 4. Constructors and the Finalizer
Event Click (AV2318) 5. Events
Protected field MainPanel 6. Properties
7. Other members grouped in a functional manner.
Const field MaximumItems
Layout 8. Private properties
Read-only static field RedValue
Method ToString  Maximum line length is 130 characters. Other private methods in calling order in-line with public
Namespace System.Drawing  Indent 4 spaces, don’t use Tabs methods.
Property BackColor  Keep one whitespace between keywords like if and the
Type Parameter TEntity expression, but don’t add whitespaces after ( and
before ). Regions only for
Camel Casing  Private fields and constants
Private field listItem  Add a whitespace around operators, like +, -, ==, etc.
 Nested classes
Variable listOfValues  Always add parentheses after keywords if, else, do,  Interface implementations (only if it’s not the main
Const variable maximumItems while, for and foreach purpose of the class)
Parameter typeName  Always put opening and closing parentheses on a new
line.
Naming
 Don’t indent object Initializers and initialize each
 Do use proper US English (AV1701)
property on a new line.
 Don’t include numbers in identifiers (AV1704)
 Don’t indent lambda statements
 Don’t prefix member fields (AV1705)
 Put the entire LINQ statement on one line, or start each
 Don’t use abbreviations (AV1706)
keyword at the same indentation.
 Name an identifier according its meaning and not its
 Add braces around comparison conditions, but don’t add
type (AV1707)
braces around a singular condition.
 Name types using nouns, noun phrases or adjective
phrases (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
Empty lines
other names (AV1712)
 Between members
 Name methods using verb-object pair (AV1720)  After the closing parentheses Important Note
 Between multi-line statements These coding guidelines are an extension to Visual
 Between unrelated code blocks Studio's Code Analysis functionalty, so make sure you
Documentation
 Around the #region keyword enable that for all your projects. Check the full
 Write comments and documentation in US English  Between the using statements of different companies. document for more details.
(AV2301)

www.csharpcodingguidelines.com
January 2011 www.dennisdoomen.net
Dennis Doomen www.avivasolutions.nl

Anda mungkin juga menyukai