You are on page 1of 6

11/30/2010

DoFactory C# Coding Standards and Na…

Join Now
C#, ASP.NET, WPF, WCF, LINQ, PAT TERNS

|

Login

home login join now

Home

References

C# Coding Standards

C# Coding Standards - and Naming Conventions
Better Code Better Career 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

products and pricing design pattern framework dofactory social

1. Naming Conventions and Style
questions explore ask question tags users public class ClientActivity { public void ClearStatistics() { //... } public void CalculateStatistics() { //... } } Why: consistent with the Microsoft's .NET Framework and easy to read. contact us training courses schedule registration sitemap about us public class UserLog { public void Add(LogEvent logEvent) { int itemCount = logEvent.Items.Count; // ... } } Why: consistent with the Microsoft's .NET Framework and easy to read.

Design Pattern FrameworkTM 4.0

do

use PascalCasing for class names and method names.

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

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

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...

training and education for professional developers

do not

use Hungarian notation or any other type identification in identifiers

// Correct int counter; string name; // Avoid int iCounter; string strName; 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. -- Instant Access -Instant Download Click here for details

do not

use Screaming Caps for constants or readonly variables

dofactory.com/…/csharp-coding-standa…

1/6

// Avoid public static const string SHIPPINGTYPE = "DropShip". bool isSaved. // Avoid UserGroup usrGrp. Assignment empAssignment. FtpHelper ftpHelper. do not use Underscores in identifiers.NET Framework. Why: consistent with the Microsoft's . // Correct public DateTime clientAppointment. Caps would grap visually too much attention. do use predefined type names instead of system type names like Int16. Ftp. public TimeSpan time_Left.11/30/2010 DoFactory C# Coding Standards and Na… // Correct public static const string ShippingType = "DropShip". XmlDocument xmlDocument. Boolean isSaved. Exception: you can prefix private static variables with an underscore. Why: consistent with the Microsoft's . Single. UIControl uiControl. dofactory. // Avoid public DateTime client_Appointment. Exceptions: abbreviations commonly used as names. UInt64. do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase) HtmlHelper htmlHelper. etc // Correct string firstName.com/…/csharp-coding-standa… 2/6 . Caps grap too much attention. int lastIndex. such as Id.NET Framework. Also avoids underline stress (inability to see underline). UriPart uriPart. Assignment employeeAssignment. Why: consistent with the Microsoft's . Why: consistent with the Microsoft's .NET Framework and prevents inconsistent abbreviations. Uri // Correct UserGroup userGroup. Xml. FtpTransfer ftpTranfer.NET Framework and makes code more natural to read (without 'slur'). Int32 lastIndex. public TimeSpan timeLeft. // Exception private DateTime _registrationDate. // Exceptions CustomerId customerId. avoid using Abbreviations. // Avoid String firstName.

// Located in Task. etc. etc) use predefined names. var stream = File. string.. Files are alphabetically sorted and partial classes remain adjacent. designer. double.com/…/csharp-coding-standa… 3/6 .generated. dofactory. string timeSheet. public class Employee { } public class BusinessLocation { } public class DocumentCollection { } Why: consistent with the Microsoft's .NET Framework.cs public partial class Task { //. e. Interface names are noun (phrases) or adjectives. do use noun or noun phrases to name a class. Why: removes clutter. } Why: consistent with the Microsoft practices.cs public partial class Task { //. // Exceptions int index = 100. public interface IShape { } public interface IShapeCollection { } public interface IGroupable { } Why: consistent with the Microsoft's .Create(path).. do name source files according to their main classes.. Exception: file names with partial classes reflect their source or purpose. bool isCompleted.11/30/2010 DoFactory C# Coding Standards and Na… Why: consistent with the Microsoft's . } // Located in Task.NET Framework and makes code more natural to read. do prefix interfaces with the letter I. particularly with complex generic types. Customer>(). do use implicit type var for local variable declarations.NET Framework and easy to remember. Exception: primitive types (int.g.. Type is easily detected with Visual Studio tooltips. var customers = new Dictionary<int?. generated.

Yellow. Left = 8 } dofactory. with static variables at the very top.Module. do use singular names for enums. } } Why: generally accepted practice that prevents the need to hunt for variable declarations.Layer.} decimal Balance {get.Product. set. do vertically align curly brackets. do declare all member variables at the top of a class.com/…/csharp-coding-standa… 4/6 . // Correct public enum Color { Red.} // Constructor public Account() { // .} DateTime DateOpened {get. // Correct class Program { static void Main(string[] args) { } } Why: Microsoft has a different standard..Group Why: consistent with the Microsoft's . Exception: bit field enums.SubModule namespace Product. Blue. Maintains good organization of your code base. set.11/30/2010 DoFactory C# Coding Standards and Na… do organize namespaces with a clearly defined structure // Examples namespace Company.Module. Bottom = 4. Top = 1.. set. Magenta. Right = 2.Module. public static decimal Reserves.NET Framework. public public public public string Number {get.} DateTime DateClosed {get. set. but developers have overwhelmingly preferred vertically aligned brackets. Cyan } // Exception [Flags] public enum Dockings { None = 0.Component namespace Product. // Correct public class Account { public static string BankName. Green.

com™. West } Why: can create confusion when relying on actual types and values.. Dime. Events. Dime. South.NET Framework and makes the code more natural to read. Exceptions.11/30/2010 DoFactory C# Coding Standards and Na… Why: consistent with the Microsoft's . Nickel. do not explicitly specify a type of an enum or values of enums (except bit fields) // Don't public enum Direction : long { North = 1. Dollar } // Correct public enum Coin { Penny. and the 'slanting do' logo are trademarks of Data & Object Factory.2010 Data & Object Factory. Nickel. East. dofactory.com/…/csharp-coding-standa… 5/6 . 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 . East = 2. LLC. we will add sections on Comments. Plural flags because enum can hold multiple values (using bitwise 'OR'). terms of service | privacy statement dofactory.NET Framework and consistent with prior rule of no type indicators in identifiers. and more. DoFactory™. West = 4 } // Correct public enum Direction { North. LLC.. Data & Object Factory™. Quarter. Quarter. Dollar } Why: consistent with the Microsoft's . South = 3. Design Pattern Framew ork™. All rights reserved. Note: Over time. do not suffix enum names with Enum // Don't public enum CoinEnum { Penny.

11/30/2010 DoFactory C# Coding Standards and Na… dofactory.com/…/csharp-coding-standa… 6/6 .