16 C# Coding Convention
Tim Svensson edited this page 2026-04-24 13:04:50 +00:00

C# Coding Convention

If coding conventions for a specific part is not documented here, please refer to Common C# code conventions

Naming Conventions

  • Use PascalCase for class names and method names.
  • Use camelCase for method arguments, local variables, and private fields.
  • Use PascalCase for constant names, both fields and local constants.
  • Interface names start with a capital I

Layout

  • Write only one statement per line.
  • Write only one declaration per line.
  • If continuation lines aren't indented automatically, indent them one tab stop (four spaces).
  • Add at least one blank line between method definitions and property definitions.
  • Use parentheses to make clauses in an expression apparent, as shown in the following code.
if ((startX > endX) && (startX > previousX))
{
    // Take appropriate action.
}

Comments

  • Use single-line comments (//) for brief explanations.
  • Avoid multi-line comments (/* */), should only be used for longer explanations where necessary.
  • Comments in the code samples aren't localized. That means explanations embedded in the code aren't translated. Longer, explanatory text should be placed in the companion article, so that it can be localized.
  • For describing methods, classes, fields, and all public members use XML comments.
  • Place the comment on a separate line, not at the end of a line of code.
  • Begin comment text with an uppercase letter.
  • End comment text with a period.
  • Insert one space between the comment delimiter (//) and the comment text, as shown in the following example.

Example:

/// <summary>
/// This class shows the coding standard, and some examples of what not to do.
/// </summary>
class HelloWorldProgram
{
    /// <summary>
    /// The classes main function. 
    /// Declares constants and then calls upon CoolMath method.
    /// Outputs resulting value from the CoolMath method.
    /// </summary>
    /// <param name="args">A string array that can take diverse arguments.</param>
    static void Main(string[] args)
    {
        const int VarX = 5; const int VarY = 10; 
        // Bad way to declare two constants, should be two lines.
        string outPut = CoolMath(VarX, VarY);

        Console.WriteLine(outPut);
    }

    /// <summary>
    /// A method to show coding standards and print hello world.
    /// It compares varX with varY and 0, and if within limits it prints "Hello world!"
    /// Otherwise it prints "Nothing here."
    /// </summary>
    /// <param name="varX">An int that gets compared with 0 and varY.</param>
    /// <param name="varY">An int that gets compared with varX.</param>
    /// <returns>Returns a string with either "Hello world!" or "Nothing here."</returns>
    static string CoolMath(int varX, int varY)
    {
        string coolName = "Hello world!";

        if ((varX < varY) && (varX > 0))
        {
            return (coolName);
        }
        else 
        {
            return "Nothing here.";
        }
    }
}