C# Coding Standard
Version 1.0

Last modified:   Monday, September 19th, 2005

C# Source Files  Each C# source file contains a single public class or interface. The root name of the file will be the name of the public class that it contains, the suffix will be .cs.

Any non-public classes in the source file must provide services that are used directly by the public class (such non-public classes may also be used by other public classes in the same package).

All C# Source files must begin with a C-style comment that lists the version information: 

/*
 *
 * Version:
 *     $Id$
 *
 * Revisions:
 *     $Log$
 */
Line Length
Lines in a source file should not have more than 80 characters. This is done since some printers may cut your line off if your source code is printed out. It is hard to grade code that is incomplete in this manner.

When a complete statement or an expression will not fit on a single 80 character line, break it according to these general principles: 

  1. Break after a comma. 
  2. Break before an operator. 
  3. Prefer higher-level breaks to lower-level breaks. 
  4. Align the new line with the beginning of the expression at the same level on the previous line. 
  5. If the above rules lead to confusing code or to code that's squished up against the right margin, just indent one additional level instead. 
Comments
The comments in your program files are very important aids for assisting programmers who are trying to understand your programs.  There are several categories of comments that you must provide. Take a look at the Microsoft XML Documentation Tutorial and the Microsoft's XML Documentation page . Writing comments in this format allows for the automatic creation of web pages that describe your programs.
XML Documentation
Comments
These are important additional comments that will be displayed in the XML documentation output.

A documentation comment line begins with three slashes ("///"). Within this, insert your comments within XML tags. Example:

    
    /// <summary>
    /// A method for rendering an entity in this game engine.
    /// </summary>
    /// <param name="theGraphics">The graphics object to render this entity in</param>
    /// <param name="x">The X coordinate to render this entity at</param>
    /// <param name="y">The Y coordinate to render this entity at</param>
    /// <param name="width">The width to scale this rendering to</param>
    /// <param name="height">The height to scale this rendering to</param>
    public virtual void render( Graphics theGraphics, int x, int y, int width, int height ){
    
  • Remember, each line in this comment begins with three slashes.
  • Insert a blank comment line between the summary and the list of tags, as shown. 
  • Insert additional blank lines to create "blocks" of related tags. So, if you were to add an exception tag to the method above, you would need a blank line (just "///") between the list of param tags and the exception tag.
Class 
Definitions
The following comments will appear before the definition of every class:
    /// <summary>
    /// A swell description of this class goes here
    /// 
    /// <list type="bullet">
    /// <item>
    /// <term>Author</term>
    /// <description>Sean Neubert (spn9006@cs.rit.edu)</description>
    /// </item>
    /// </list>
    /// 
    /// </summary>
The elements of a class or interface declaration should appear in the following order:
  1. Class (static) variables 
  2. Instance variables 
  3. Constructors 
  4. Methods 
Public variables should be listed first, followed by protected, then internal protected, internal, and finally the private variables.  Methods should be grouped by functionality.
Namespaces You should put your CSharp code in a namespace. Before your class declaration in a source file, you should declare a namespace. Use "RITCS" as your namespace unless you have a more appropriate namespace name. The structure of the file is like this:
namespace RITCS {
    class someclass {


    }
}
Method 
Headers
The following comments will appear before every method (including main): 
    /// <summary>
    /// Summary of this method.
    /// </summary>
    ///
    /// <param name="name of parameter">description</param>
    ///
    /// <exception cref="exception type">description of why it is thrown</exception>

param:  The param tag contains an attribute called name (not type) of the parameter, followed by a description of the parameter inside of the tag. The name and data type always start with a lowercase letter. The description is most usually a phrase, starting with a lowercase letter and ending without a period, unless it contains a complete sentence. 

return:  The return tag is followed by a description of the return value. Whenever possible, detailed information (such as returns -1 when an out-of-bounds argument is supplied) should be provided.

exception:  An exception tag should be included for any exceptions you throw. The exception tag has an attribute named "cref". This should contain the name of the type of exception thrown. The content of the tag should describe what conditions are present when the exception is thrown.
Properties With a property, you comment it just like a method, only you don't provide parameter tags since properties don't take parameters. It is acceptable for properties to be uppercase, although variables in general should start with a lower case letter. Example:
    /// 
    /// Gets a number
    /// 
    private int number=0;
    public int Number {
        get {
            return number;
        }
	set {
	    number = value;
	}
    }

Declaration and Code Comments In addition to the class and method headers, which describe your class and its interface for a user or client of the class, you must provide additional comments to describe the program code that you have written.  Programmers who must modify your code at some point will use these comments to quickly understand the logic and operations performed.  Someone reading one of your methods should not be required to read actual program statements to gain an understanding of the logic and operations performed in the method.  You must write additional comments that provide this information.

Any non-trivial method (i.e.it is longer than five lines of code) requires comments.  Multiple comments may be needed to adequately describe the method.

  • All code and declaration comments will be started with the "//" (double slash).
  • If the comment requires multiple lines each will be started with the "//".
  • Declaration comments provide a description of the data object declared.
    •  
    • They can appear at the end of the declaration line provided the comment does not extend the line beyond 80 characters; otherwise, they appear preceeding the declaration and indented at the same level as the declaration with a blank line preceeding the comment.
    • If there are multiple declaration lines in a row all comments will start in the same column.

    •  
  • A comment for code appears on a line by itself preceding the code to which it refers.
  • A blank line will precede the comment and for clarity, a blank line can also optionally follow the comment.
  • The indentation will match the code following the comment line(s).
Note: these declarations and comments will not appear in the web-based documentation generated by NDoc.
Identifiers  Class names begin with a capital letter; method and variable names begin with a lowercase letter. The one exception to this is properties: properties may begin with an upper case letter. The remaining letters are lowercase.  The second and subsequent words of multi-word identifiers are also capitalized, but not separated by underscores. 

Constants are given names to indicate their use and meaning. These names will be all upper case letters. The words of multi-word identifiers are separated from one another by underscores ("_"). 

Indenting  The indentation of your program code provides a strong visual indication of the structure of your methods.  This greatly aids in understanding the code.   Consistency is the most important characteristic of indenting style.  It is recommended that you use an indent no smaller than 2 and no larger than 8 spaces. Tabs must not be used to indent since different editors treat tabs in diferent ways. 

When using braces ( "{" or "}" ) in a C# program, adhere to the following:

  • The opening brace should be at the end of the line that begins the compound statement or declaration; the closing brace should begin a line and be indented to the same level as the line holding the opening brace. 
  • The enclosed statements should be indented one more level than the lines containing the opening and closing braces.
  • Braces are used around all statements, even single statements, when they are part of a control structure, such as a if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces. 
Horizontal 
Spacing
Like indentation, consistent spacing around special characters aids in the understanding of your program code.  A suggested style for horizontal spacing is:
  • When using parenthesis to delimit argument lists, there should be no space before the left parenthesis ('('), one space after it, and one space before the right (')') parenthesis.  For empty argument lists no spaces are placed between the parentheses.
  • Commas should always be followed by a space, but never preceded by one.
  • Parenthesis in expressions should have a space both before and after.
  • Binary operators should be surrounded by a space on each side; no space separates an unary operator from its operand.
  • There will always be at least one blank line between the end of a method definition and the method header for the following method. 
Example
Program
/*
 *
 * Version:
 *     $Id$
 *
 * Revisions:
 *     $Log$
 */

// Import statements are placed here
using System;

namespace RITCS {

	/// <summary>
	/// This program produces a conversion table from Celsius to
	/// Fahrenheit for values from 0 to 99.  This program appears in
	/// "C# Gently" by J M Bishop.
	/// 
	/// <list type="bullet">
	/// 
	/// <item>
	/// <term>Author</term>
	/// <description>J M Bishop</description>
	/// </item>
	/// 
	/// <item>
	/// <term>Author</term>
	/// <description>Paul Tymann</description>
	/// </item>
	/// 
	/// <item>
	/// <term>Author</term>
	/// <description>Sean Neubert - CSharp port</description>
	/// </item>
	/// 
	/// </list>
	/// 
	/// </summary>
	class ConversionTable {
		const int    COLS_PER_LINE = 5;    // number of temp. cols
		const int    MAX_LINE_NO = 20;     // number of rows of temp.
		const string GAP = "\t";           // white space between cols

		/// <summary>
		/// The main program.
		/// </summary>
		/// <param name="args">command line arguments (ignored)</param>
		public static void Main( string [] args ){
			// print out the table heading

			Console.WriteLine( "\t\tTemperature Conversion Table" );
			Console.WriteLine( "\t\t============================" );
			Console.WriteLine();

			// print a heading for each column of output

			for ( int col = 0; col < COLS_PER_LINE; col++ ){
				Console.Write( "C  F" + GAP );
			}

			Console.WriteLine();

			// print out the lines of the table

			for ( int row = 0; row < MAX_LINE_NO; row++ ){
				printRow( row );
			}
		}

		/// <summary>
		/// Given the row, calculate the Celsius values for the line
		/// and display them with their Fahrenheit equivalents.
		/// </summary>
		/// <param name="row">row to print</param>
		static void printRow( int row ) {
			for ( int col = 0; col < COLS_PER_LINE; col++ ) {
				int degCelsius = row * COLS_PER_LINE + col;

				Console.Write( degCelsius + " " );
				Console.Write( fahrenheit( degCelsius ) + GAP );
			}

			Console.WriteLine();
		}

		/// <summary>
		/// Convert degrees Celsius to degrees Fahrenheit
		/// </summary>
		/// <param name="celsius">degrees in Celsius</param>
		/// <returns>degrees in Fahrenheit</returns>
		static int fahrenheit( int celsius ) {
			return (int)Math.Round( (double)(celsius * 9 / 5 + 32) );
		}

	} // ConversionTable
}

Example
Statements
Switch Statement:
char letter = 'B';
double grade;

switch ( letter ) {
case 'A':
case 'a':
  grade = 4.0;
  break;
case 'B':
case 'b':
  grade = 3.0; 
  break;
case 'C':
case 'c':
  grade = 2.0;
  break;
case 'D':
case 'd':
  grade = 1.0;
  break;
default: 
  grade = 0;
  break;
}

While Statement:
int number = 1;
int sum = 0;

while ( number < 100 ) {
  sum = sum + number;
  number++;
}

Do While Statement:
int sum = 0; 
int number = 1;

do {
  sum += number;
  number++;
} while ( number < 100 );

If, if-else, if else-if else Statements:
int number = 1;

if ( number < 100 ) {
  number = number + 100;
}

if ( number < 100 ) {
  number = number + 100;
} else {
  number = number * 10;
}

if ( number < 100 ) {
  number = number + 100;
} else if ( number > 100 ) {
  number = number * 10;
} else {
  number = number / 2;
}