You are on page 1of 5

2012.08.14.

DoFactory C# Coding Standards and Naming Conventions

Join Now
C#, ASP.NET, WPF, WCF, LINQ, PATTERNS

Login

home login join now

Home

References

C# Coding Standards

C# Coding Standards - and Naming Conventions


Better Code Better Career

products and pricing design pattern framework javascript pattern framework database pattern framework adaptive data models

Here are our C# coding standards, naming conventions, and best practices. Use these in your own projects and/or adjust these to your own needs.

Better Lifestyle

1. Naming Conventions and Style

Design Pattern FrameworkTM 4.0

design patterns javascript tutorial sql + database tutorial all tutorials

do

use PascalCasing for class names and method names.

connection strings visual studio shortcuts c# coding standards html color codes all references

p u b l i cc l a s sC l i e n t A c t i v i t y { p u b l i cv o i dC l e a r S t a t i s t i c s ( ) { / / . . . } p u b l i cv o i dC a l c u l a t e S t a t i s t i c s ( ) { / / . . . } } Why: consistent with the Microsoft's .NET Framework and easy to read.

C# and VB Here's what you get: Gang of Four Patterns Head First Patterns

explore questions ask question tags users p u b l i cc l a s sU s e r L o g { p u b l i cv o i dA d d ( L o g E v e n tl o g E v e n t ) { i n ti t e m C o u n t=l o g E v e n t . I t e m s . C o u n t ; / /. . . } } Why: consistent with the Microsoft's .NET Framework and easy to read.

do

use camelCasing for method arguments and local variables.

Enterprise Patterns SOA Patterns WPF Best Practices WCF Best Practices LINQ Best Practices Model View Controller Model View Presenter Model View ViewModel More...

contact us training courses schedule registration sitemap about us

training and education for professional developers

do not

use Hungarian notation or any other type identification in identifiers

/ /C o r r e c t i n tc o u n t e r ; s t r i n gn a m e ; / /A v o i d i n ti C o u n t e r ; s t r i n gs t r N a m e ; Why: consistent with the Microsoft's .NET Framework and Visual Studio IDE makes determining types very easy (via tooltips). In general you want to avoid type indicators in any identifier. Upgrade your .NET skills Click here for details -- Instant Access --

do not

use Screaming Caps for constants or readonly variables

Instant Download

www.dofactory.com/reference/csharp-coding-standards.aspx

/ /C o r r e c t p u b l i cs t a t i cc o n s ts t r i n gS h i p p i n g T y p e=" D r o p S h i p " ;

1/5

2012.08.14.

DoFactory C# Coding Standards and Naming Conventions


p u b l i cs t a t i cc o n s ts t r i n gS h i p p i n g T y p e=" D r o p S h i p " ; / /A v o i d p u b l i cs t a t i cc o n s ts t r i n gS H I P P I N G T Y P E=" D r o p S h i p " ; Why: consistent with the Microsoft's .NET Framework. Caps grap too much attention.

avoid

using Abbreviations. Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri

/ /C o r r e c t U s e r G r o u pu s e r G r o u p ; A s s i g n m e n te m p l o y e e A s s i g n m e n t ; / /A v o i d U s e r G r o u pu s r G r p ; A s s i g n m e n te m p A s s i g n m e n t ; / /E x c e p t i o n s C u s t o m e r I dc u s t o m e r I d ; X m l D o c u m e n tx m l D o c u m e n t ; F t p H e l p e rf t p H e l p e r ; U r i P a r tu r i P a r t ;

Why: consistent with the Microsoft's .NET Framework and prevents inconsistent abbreviations.

do

use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase)

H t m l H e l p e rh t m l H e l p e r ; F t p T r a n s f e rf t p T r a n f e r ; U I C o n t r o lu i C o n t r o l ; Why: consistent with the Microsoft's .NET Framework. Caps would grap visually too much attention.

do not

use Underscores in identifiers. Exception: you can prefix private static variables with an underscore.

/ /C o r r e c t p u b l i cD a t e T i m ec l i e n t A p p o i n t m e n t ; p u b l i cT i m e S p a nt i m e L e f t ; / /A v o i d p u b l i cD a t e T i m ec l i e n t _ A p p o i n t m e n t ; p u b l i cT i m e S p a nt i m e _ L e f t ; / /E x c e p t i o n p r i v a t eD a t e T i m e_ r e g i s t r a t i o n D a t e ; Why: consistent with the Microsoft's .NET Framework and makes code more natural to read (without 'slur'). Also avoids underline stress (inability to see underline).

do

use predefined type names instead of system type names like Int16, Single, UInt64, etc

/ /C o r r e c t s t r i n gf i r s t N a m e ; i n tl a s t I n d e x ; b o o li s S a v e d ; / /A v o i d S t r i n gf i r s t N a m e ; I n t 3 2l a s t I n d e x ; B o o l e a ni s S a v e d ;

www.dofactory.com/reference/csharp-coding-standards.aspx

2/5

2012.08.14.

DoFactory C# Coding Standards and Naming Conventions


Why: consistent with the Microsoft's .NET Framework and makes code more natural to read.

do

use implicit type var for local variable declarations. Exception: primitive types (int, string, double, etc) use predefined names.

v a rs t r e a m=F i l e . C r e a t e ( p a t h ) ; v a rc u s t o m e r s=n e wD i c t i o n a r y < i n t ? ,C u s t o m e r > ( ) ; / /E x c e p t i o n s i n ti n d e x=1 0 0 ; s t r i n gt i m e S h e e t ; b o o li s C o m p l e t e d ; Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.

do

use noun or noun phrases to name a class.

p u b l i cc l a s sE m p l o y e e { } p u b l i cc l a s sB u s i n e s s L o c a t i o n { } p u b l i cc l a s sD o c u m e n t C o l l e c t i o n { } Why: consistent with the Microsoft's .NET Framework and easy to remember.

do

prefix interfaces with the letter I . Interface names are noun (phrases) or adjectives.

p u b l i ci n t e r f a c eI S h a p e { } p u b l i ci n t e r f a c eI S h a p e C o l l e c t i o n { } p u b l i ci n t e r f a c eI G r o u p a b l e { } Why: consistent with the Microsoft's .NET Framework.

do

name source files according to their main classes. Exception: file names with partial classes reflect their source or purpose, e.g. designer, generated, etc.

/ /L o c a t e di nT a s k . c s p u b l i cp a r t i a lc l a s sT a s k { / / . . . } / /L o c a t e di nT a s k . g e n e r a t e d . c s p u b l i cp a r t i a lc l a s sT a s k { / / . . . } Why: consistent with the Microsoft practices. Files are alphabetically sorted and partial classes remain adjacent.

www.dofactory.com/reference/csharp-coding-standards.aspx

do

organize namespaces with a clearly defined structure

3/5

2012.08.14.

DoFactory C# Coding Standards and Naming Conventions

do

organize namespaces with a clearly defined structure

/ /E x a m p l e s n a m e s p a c eC o m p a n y . P r o d u c t . M o d u l e . S u b M o d u l e n a m e s p a c eP r o d u c t . M o d u l e . C o m p o n e n t n a m e s p a c eP r o d u c t . L a y e r . M o d u l e . G r o u p Why: consistent with the Microsoft's .NET Framework. Maintains good organization of your code base.

do

vertically align curly brackets.

/ /C o r r e c t c l a s sP r o g r a m { s t a t i cv o i dM a i n ( s t r i n g [ ]a r g s ) { } } Why: Microsoft has a different standard, but developers have overwhelmingly preferred vertically aligned brackets.

do

declare all member variables at the top of a class, with static variables at the very top.

/ /C o r r e c t p u b l i cc l a s sA c c o u n t { p u b l i cs t a t i cs t r i n gB a n k N a m e ; p u b l i cs t a t i cd e c i m a lR e s e r v e s ; p u b l i cs t r i n gN u m b e r{ g e t ;s e t ; } p u b l i cD a t e T i m eD a t e O p e n e d{ g e t ;s e t ; } p u b l i cD a t e T i m eD a t e C l o s e d{ g e t ;s e t ; } p u b l i cd e c i m a lB a l a n c e{ g e t ;s e t ; } / /C o n s t r u c t o r p u b l i cA c c o u n t ( ) { / /. . . } } Why: generally accepted practice that prevents the need to hunt for variable declarations.

do

use singular names for enums. Exception: bit field enums.

/ /C o r r e c t p u b l i ce n u mC o l o r { R e d , G r e e n , B l u e , Y e l l o w , M a g e n t a , C y a n } / /E x c e p t i o n [ F l a g s ] p u b l i ce n u mD o c k i n g s { N o n e=0 , T o p=1 , R i g h t=2 , B o t t o m=4 , L e f t=8 } Why: consistent with the Microsoft's .NET Framework and makes the code more natural to read. Plural flags because enum can hold multiple values (using bitwise 'OR').

www.dofactory.com/reference/csharp-coding-standards.aspx

4/5

2012.08.14.

DoFactory C# Coding Standards and Naming Conventions

do not

explicitly specify a type of an enum or values of enums (except bit fields)

/ /D o n ' t p u b l i ce n u mD i r e c t i o n:l o n g { N o r t h=1 , E a s t=2 , S o u t h=3 , W e s t=4 } / /C o r r e c t p u b l i ce n u mD i r e c t i o n { N o r t h , E a s t , S o u t h , W e s t } Why: can create confusion when relying on actual types and values.

do not

suffix enum names with Enum

/ /D o n ' t p u b l i ce n u mC o i n E n u m { P e n n y , N i c k e l , D i m e , Q u a r t e r , D o l l a r } / /C o r r e c t p u b l i ce n u mC o i n { P e n n y , N i c k e l , D i m e , Q u a r t e r , D o l l a r } Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.

Note: Over time, we will add sections on Comments, Events, Exceptions, and more...

return to top

home | join | login | design patterns | design pattern framew ork | pattern topics | tags | users | new topic | search | faq resources | connection strings | visual studio shortcuts | courses | schedule | registration | sitemap | about us Copyright 2001 - 2012 Data & Object Factory, LLC. All rights reserved. Data & Object Factory, dofactory.com, DoFactory, Design Pattern Framew ork, and the 'slanting do' logo are trademarks of Data & Object Factory, LLC. terms of service | privacy statement

www.dofactory.com/reference/csharp-coding-standards.aspx

5/5

You might also like