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
ifand 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,forandforeach, 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
fromexpressions 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"))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;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.
- Private fields and constants
- Public constants
- Public read-only static fields
- Events
- Public Properties
- Constructors and the Finalizer
- 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 · CS-06.4 · MUSTRegions 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