Engineering/Coding Standards/C#/C# Layout/

C# Layout Standards · CS-06

Use a common layout · CS-06.1 · MUST

  • Keep the length of each line under 130 characters.
  • Keep one whitespace between keywords like if and the expression (Roslyn Analyzer Rule SA1000), but don’t add whitespaces after ( and before ) such as:
if (condition == null)
  • Add a whitespace around operators, like +, -, ==, etc. (Roslyn Analyzer Rule SA1003)
  • Always succeed the keywords if, else, do, while, for and foreach, with opening and closing braces (Roslyn Analyzer Rule ACL1006).
  • Always put opening and closing braces on a new line (Roslyn Analyzer Rule SA1500).
  • Put the entire LINQ statement on one line, or start each keyword at the same indentation (Roslyn Analyzer Rule SA1103), like this:
var query = from product in products where product.Price > 10 select product;

or

var query =
    from product in products
    where product.Price > 10
    select product;
  • Start the LINQ statement with all the from expressions and don’t interweave them with where restrictions.
  • Add parentheses around every comparison condition, but don’t add parentheses around a singular condition. For example
if (!String.IsNullOrEmpty(str) && (str != "new"))
  • Add an empty line between multi-line statements, between members, after the closing parentheses, and between unrelated code blocks (Roslyn Analyzer Rules SA1513 and SA1516).

Order and group namespaces according the company · CS-06.2 · MUST

// Microsoft namespaces are first
using System;
using System.Collections;
using System.XML;

// Then any other namespaces in alphabetic order
using AvivaSolutions.Business;
using AvivaSolutions.Standard;

using Telerik.WebControls;
using Telerik.Ajax;

Roslyn Analyzer Rules SA1208 and SA1210


Place members in a well-defined order · CS-06.3 · MUST

Maintaining a common order allows other team members to find their way in your code more easily. In general, a source file should be readable from top to bottom, as if you are reading a book. This prevents readers from having to browse up and down through the code file.

  1. Private fields and constants
  2. Public constants
  3. Public read-only static fields
  4. Events
  5. Public Properties
  6. Constructors and the Finalizer
  7. Public and private methods, with related methods grouped together in calling order
    • For example, if three private methods are all called from a public method, the private methods should be located immediately beneath the public method in the order in which they are called

Be reluctant with #regions · CS-06.4 · MUST

Regions can be helpful, but can also hide the main purpose of a class. Therefore, use #regions only for:

  • Interface implementations (only if the interface is not the main purpose of that class)
  • Grouping related test methods, if a test class has a large number of methods

Roslyn Analyzer Rule SA1124


Use global usings · CS-06.5 · SHOULD

A using statement can be prefixed with global to apply it to all files in a project. For example to automatically use System in all files, add the following statement to a file in the project:

global using System;

For consistency, a dedicated file must be used to store all global usings for a project. This file should be called GlobalUsings.cs and be located in the root of the project.


Use implicit usings · CS-06.6 · SHOULD

The ‘implicit usings’ feature implicitly adds certain using statements to all files based on the project type. It can be enabled in the .csproj:

<PropertyGroup>
    <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Use file-scoped namespaces · CS-06.7 · MUST

For example, rather than a namespace block like this:

namespace Richard.Is
{
    public class Awesome
    {
    }
}

prefer a file-scoped namespace like this:

namespace Richard.Is;

public class Awesome
{
}

Roslyn Analyzer Rule IDE0161