1. Introduction First, read the .NET Framework Design Guidelines.

Almost all naming conventions, casing rules, etc., are spelled out in this document. Unlike the Design Guidelines document, you should treat this document as a set of suggested guidelines. These generally do not effect the customer view so they are not required. 2. Style Guidelines 2.1 Tabs & Indenting Tab characters (\0x09) should not be used in code. All indentation should be done with 4 space characters. 2.2 Bracing Open braces should always be at the beginning of the line after the statement that begins the block. Contents of the brace should be indented by 4 spaces. For example: if (someExpression) { DoSomething(); } else { DoSomethingElse(); } “case” statements should be indented from the switch statement like this: switch (someExpression) { case 0: DoSomething(); break; case 1: DoSomethingElse(); break; case 2: {

it is hoped that most routines will have comments reflecting the programmer’s intent and approach. } break. This increases code readability and maintainability.1 Copyright notice Each file should start with a copyright notice. } } } It is suggested that all control structures (if. public int Bar { get { return bar. etc. you don’t want to use triple-slash doc comments. if from reading the comments alone. Even for single statement blocks. for. public class Foo { int bar. } 2. DoAnotherThing(n).3 Single line statements Single line statements can have braces that begin and end on the same line. 2.) use braces. } set { bar = value. It would be ideal. for (int i=0. } Braces should never be considered optional. To avoid errors in doc comment builds. algorithmic overview.4 Commenting Comments should be used to describe intention. 2.int n = 1. While there are no minimum comment requirements and certainly some very small routines need no commenting at all. and/or logical flow.4. while. but . i<100. but it is not required. someone other than the author could understand a function’s intended behavior and general operation. you should always use braces. i++) { DoSomething(i).

use the <include> tag.2 Documentation Comments All methods should use XML doc comments. For internal dev comments. Final text will vary by product (you should contact legal for the exact text). // </copyright> //----------------------------------------------------------------------2. it is common that you would want to move the XML documentation to an external file – for that.uex' path='docs/doc[@for="Foo. public class Foo { /// <summary>Public stuff about the method</summary> /// <param name=”bar”>What a neat parameter!</param> /// <devdoc>Cool internal stuff!</devdoc> /// public void MyMethod(int bar) { … } } However.3 Comment Style .MyMethod"]/*' /> /// public void MyMethod(int bar) { … } } UNDONE§ there is a big doc with all the comment tags we should be using… where is that? 2. the <devdoc> tag should be used.cs" company="Microsoft"> // Copyright (c) Microsoft Corporation.4.using XML makes the comments easy to replace in the future. public class Foo { /// <include file='doc\Foo.4. All rights reserved. but should be similar to: //----------------------------------------------------------------------// <copyright file="ContainerControl.

Right: CreateFoo() Wrong: CreateFoo () Do not use spaces inside brackets.Read(myChar. Here are some examples: // This is required for WebClient to work through the proxy GlobalProxySelection. Comments can be placed at the end of a line when space allows: public class SomethingUseful { private int itemHash. Here are some guidelines for the use of space characters within code: • • • • • • Do use a single space after a comma between function arguments.Select = new WebProxy("http://itgproxy").In. 0. 1).In. Right: Console. Do not use a space after the parenthesis and function arguments Right: CreateFoo(myChar.0.5 Spacing Spaces improve readability by decreasing code density. Wrong: x = dataArray[ index ]. // static member } 2. // instance member private static bool hasDoneSomething. // Create object to access Internet resources // WebClient myClient = new WebClient(). Wrong: Console. 1) Wrong: CreateFoo( myChar. 0.The // (two slashes) style of comment tags should be used in most situations. Where ever possible. 0. Right: x = dataArray[index]. 1 ) Do not use spaces between a function name and parenthesis. Do use a single space before flow control statements Right: while (x == y) Wrong: while(x==y) Do use a single space before and after comparison operators Right: if (x == y) Wrong: if (x==y) .1). place comments above the code instead of beside it.Read(myChar.

2. These methods are potentially dangerous and any caller of these methods must do a full security review to ensure that the usage is safe and protected as no stack walk will be performed.) is to produce a consistent source code appearance. or delegates with any letter The reasons to extend the public rules (no Hungarian. These methods are safe and can be used fairly safely and the caller isn’t needed to do full security reviews even though no stack walk will be performed.6 Naming Follow all . property. etc. In addition a goal is to have clean readable source. class NativeMethods { private NativeMethods() {} . m_. event.” in VB.” in C# and “Me. If you want to distinguish between local and member variables you should use “this. UnsafeNativeMethods – Has suppress unmanaged code attribute. classes. these are methods that can be used anywhere because a stack walk will be performed. Code legibility should be a primary goal. etc. Do use camelCasing for member variables Do use camelCasing for parameters Do use camelCasing for local variables Do use PascalCasing for function. and class names Do prefix interfaces names with “I” Do not prefix enums.NET. s_.).1 Interop Classes Classes that are there for interop wrappers (DllImport statements) should follow the naming convention below: • • • NativeMethods – No suppress unmanaged code attribute. no prefix for member variables.2. SafeNativeMethods – Has suppress unmanaged code attribute.7 Naming Conventions 2. Highlights of these include: • • • • • • • • Do not use Hungarian notation Do not use a prefix for member variables (_.7.NET Framework Design Guidelines for both internal and external members.

} [SuppressUnmanagedCode] class UnsafeNativeMethods { private UnsafeNativeMethods() {} [DllImport(“user32”)] internal static extern void CreateFile(string fileName). Private interface implementations. Nested types) Using statements should be inside the namespace declaration. } [SuppressUnmanagedCode] class SafeNativeMethods { private SafeNativeMethods() {} [DllImport(“user32”)] internal static extern void MessageBox(string text).Windows. Constructors. Methods. although multiple internal classes are allowed Source files should be given the name of the public class in the file Directory names should follow the namespace for the class For example.cs”… • • Classes member should be alphabetized. 2.Forms. Properties. In addition a private constructor should be provided to prevent instantiation. namespace MyNamespace { . and all methods must be internal. and grouped into sections (Fields.[DllImport(“user32”)] internal static extern void FormatHardDrive(string driveName). Events. } All interop classes must be private.Control” in “System\Windows\Forms\Control.8 File Organization • • • Source files should contain only one public type. I would expect to find the public class “System.

DoSomething() { DoSomething(). } // nested types class NestedType { … } } } . public class MyClass : IFoo { // fields int foo. // constructors public MyClass() { … } // properties public int Foo { get { … } set { … } } // events public event EventHandler FooChanged { add { … } remove { … } } // methods void DoSomething() { … } void FindSomethind() { … } //private interface implementations void IFoo.using System.