# C# Layout Standards · CS-06

## Standard Metadata

- Area: C#
- Owner: Backend Steering group
- Last reviewed: 2026-09-09
- Next review due from: 2026-12-01
- Enforcement: CodeAnalysis / Rosyln Analyser / code review
- Status: Active

## 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](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1000.md)**), but don't add whitespaces after `(` and before `)` such as:

```csharp
if (condition == null)
```

- Add a whitespace around operators, like `+`, `-`, `==`, etc. (**Roslyn Analyzer Rule [SA1003](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1003.md)**)
- Always succeed the keywords `if`, `else`, `do`, `while`, `for` and `foreach`, with opening and closing braces (**Roslyn Analyzer Rule [ACL1006](https://github.com/audaciaconsulting/Audacia.CodeAnalysis/blob/master/dotnet-roslyn/analyzers/Audacia.CodeAnalysis.Analyzers/README.md#acl1006---code-block-does-not-have-braces)**).
- Always put opening and closing braces on a new line (**Roslyn Analyzer Rule [SA1500](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1500.md)**).
- Put the entire LINQ statement on one line, or start each keyword at the same indentation (**Roslyn Analyzer Rule [SA1103](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1103.md)**), like this:

```csharp
var query = from product in products where product.Price > 10 select product;
```

or

```csharp
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

```csharp
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](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1513.md) and [SA1516](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1516.md)**).

---

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

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

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

using Telerik.WebControls;
using Telerik.Ajax;
```

**Roslyn Analyzer Rules [SA1208](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md) and [SA1210](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1210.md)**

---

## 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
1. Public constants
1. Public read-only static fields
1. Events
1. Public Properties
1. Constructors and the Finalizer
1. 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 `#region`s · 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](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1124.md)**

---

## 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:

```csharp
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:

```xml
<PropertyGroup>
    <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
```

---

## Use file-scoped namespaces · CS-06.7 · **MUST**

For example, rather than a `namespace` block like this:

```csharp
namespace Richard.Is
{
    public class Awesome
    {
    }
}
```

prefer a file-scoped `namespace` like this:

```csharp
namespace Richard.Is;

public class Awesome
{
}
```

**Roslyn Analyzer Rule IDE0161**
