| 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: /* |
|---|---|
|
|
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:
|
|
|
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. |
|
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 ){
|
| 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:
|
| 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>
|
| 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:
/// |
| 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.
|
| 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:
|
| Horizontal
Spacing |
Like indentation, consistent spacing around special characters aids
in the understanding of your program code. A suggested style for
horizontal spacing is:
|
| 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
}
|
|
Statements |
Switch Statement:
char letter = 'B';While Statement: int number = 1;Do While Statement: int sum = 0;If, if-else, if else-if else Statements: int number = 1; |