Anda di halaman 1dari 2

C# 3.

0 Coding Guidelines Quick Reference


Design & Usage

Goals Don’t add extension methods to the same namespace Avoid methods with more than 5 arguments (AV1561)
Prevent common mistakes and pitfalls as the extended class (AV1070) Do always check the result of an as operation (AV1570)
Prevent language constructs that can be Do evaluate the result of a LINQ expression before
misinterpreted
Promote object oriented development returning it (AV1076) Usage
Ensure that all source code has the same look-and- Do use C# types instead of the types from the System
feel
Maintainability namespace (AV2201)
Improve readability and reviewability
Avoid using names that can be mistaken with other Don’t hardcode strings that are presented to end-users
Design names (AV1501) (AV2206)
Don’t hide inherited members with the new keyword Do name a source file to the class it contains (AV1506) Do build with the highest warning level (AV2210)
(AV1010) Do limit the contents of a source code file to one class Avoid LINQ for simple expressions (AV2220)
It should be possible to treat a derived object as if it (AV1507) Do use Lambda expressions instead of delegates
were a base class object (AV1010) Don’t use "magic numbers" (AV1515) (AV2221)
Do allow properties to be set in any order (AV1015) Do use var only when the type is very obvious
Don’t create a constructor that does not yield a fully (AV1520)
initialized object (AV1020) Do initialize variables at the point of declaration, if
Don’t return an array (AV1025) possible (AV1521)
String and array properties should never return a null Favor Object and Collection Initializers over separate
reference (AV1027) statements (AV1523)
Provide a rich and meaningful exception message text Do use an enumeration instead of a list of strings if the
list of values is finite (AV1526) Important Note
(AV1040)
These coding guidelines are an extension to Visual
Don’t swallow errors by catching non-specific exceptions Do use a block with all flow control keywords, even if it is Studio 2008's Code Analysis functionalty, so make sure
(AV1042) empty (AV1535) you enable that for all you project. Check the full
document for more details.
Prefer throwing existing exceptions (AV1043) Do code every if-else if statement with an else-part
Do always check an event handler delegate for null (AV1537)
(AV1051) Avoid multiple return statements (AV1540)
Feedback
Do use a protected virtual method to raise each event Prefer conditional statements instead of simple if-else
This document has been compiled using many
(AV1052) constructs (AV1546) contributions from (ex) colleagues, sources from the
Do only implement casts that operate on the complete Do explicitly define the scope of a type or member Internet, and many years of developing in C#. If you
have questions, comments or suggestions, please let
object (AV1060) (AV1550)
us know by sending me an email at
Avoid casting to and from System.Object in code that Do implement the most complete overload of a method dennis.doomen@avivasolutions.nl. We will try to revise
uses generics (AV1062) or constructor and call it from the other overloaded and republish this document with new insights,
experiences and remarks on a regular basis.
methods or constructors (AV1551)

March 2009 www.avivasolutions.nl


Dennis Doomen blog.avivasolutions.nl
C# 3.0 Coding Guidelines Quick Reference
Naming & Layout

Naming Class member order


Documentation 1. Private fields
Do use proper US English (AV1701)
2. Public read-only static fields
Don’t prefix member fields (AV1705) Do use proper US English (AV1701) 3. Constructors and the Finalizer
Do name an identifier according its meaning and not its Do use XML tags for documenting types and members 4. Events
(AV2305) 5. Properties
type (AV1707)
6. Other members grouped in a functional manner.
Do name classes, interfaces and value types with Do write XML documentation with the caller in mind a. Control event handlers
nouns, noun phrases or adjective phrases (AV1708) (AV2306) b. Page lifecycle handlers
Don’t repeat the name of a class or enumeration in its Do write MSDN-style documentation (AV2307) c. Interface implementations
7. Private properties
members (AV1710) Do write comments that explain the purpose (the why) of
Do name methods using verb-object pair (AV1720) a code block (AV2316) Other private methods in calling order in-line with public
methods.
Do use a verb or verb phrase to name an event
(AV1735) Layout
Do prefix an event handler with On (AV1738) Do write comments that explain the purpose of a code Member modifier order
public, protected, internal, private,
Group extension methods in a class suffixed with block (AV2316) partial, new, abstract, virtual,
Extensions (AV1745) Do order and group namespaces by company preceded override, sealed, static, readonly,
by Microsoft namespacves (AV2402) extern, volatile, const

Be reluctant with #regions


Pascal Casing
Don’t exceed a line length of 130 characters (AV2411) Empty lines
Class, Struct AppDomain
Interface IBusinessService Do insert space after if, switch, while, for, Between members
Enumeration type ErrorLevel foreach, catch, lock and using (AV2412) After the closing parentheses
Enumeratiion values FatalError Between unrelated code blocks
Do put parentheses on a new line (AV2413) Around the #region keyword
Event ValueChange
Protected field MainPanel Between the using statements of the same company.
Const field MaximumItems
Read-only static field RedValue
Regions
Method ToString
Private fields
Namespace System.Drawing
Control event handlers
Property BackColor
Page lifecycle handlers
Generic Type Parameter TEntity
Nested classes
Camel Casing Helper classes (in the same file)
Private field listItem Interface implementations (only if the class contains
other members)
Local variable listOfValues
Parameter typeName

March 2009 www.avivasolutions.nl


Dennis Doomen blog.avivasolutions.nl