JetBrains Rider 2018.2 Help

Code Style. C#

File | Settings | Editor | Code Style | C# for Windows and Linux
JetBrains Rider | Preferences | Editor | Code Style | C# for macOS
Ctrl+Alt+S icons general settings svg


On this page of JetBrains Rider settings, you can configure various aspects of code style in C#. Code style preferences are grouped in the following tabs:

Tabs, Indents, Alignment

This tab helps you specify how JetBrains Rider should format indents in your code when you type or when you reformat existing code.

Indentation-settings source

By default, JetBrains Rider uses Visual Studio preferences for tabs and indents (Tools | Options | Text Editor | All Languages | Tabs and Tools | Options | Text Editor | [language] | Tabs). However, if you prefer to share formatter preferences with your team using JetBrains Rider's layered settings, you can clear the Use indent style and size from Visual Studio check box, and then configure and save your indentation preferences to the team-shared setting layer.

If there are .editorconfig files that affect your solution, preferences on this and other tabs with code style settings could be overridden by EditorConfig styles. You will see a yellow warning if at least one preference on the page is overridden by EditorConfig styles for the current file, each overridden preference will also be highlighted with yellow. For example:

Code formatting options overridden by EditorConfig styles

Combining indentation and alignment in multiline constructs

If your preferences say that whitespaces should be used for indents, JetBrains Rider will also use whitespaces to align multiple constructs — for example, when function parameters on multiple lines are aligned by the first parameter:

void foo(int firstParameter, int secondParameter) { }

However, when you choose to use tabs for indents, there could be different ways for aligning multi-line constructs. JetBrains Rider lets you choose which of these ways to use when creating new and reformatting existing code.

You can configure preferences for aligning multi-line constructs with the How to align when tabs are used for indents option.

  • Use spaces (recommended, looks aligned on any tab size)
    When this option is selected, JetBrains Rider uses tabs for indents and spaces for alignment:

    general formatter style1
    This is recommended option because code aligned with second and third options may lose alignment when viewed in an editor with a different tab size.

  • Only use tabs (inaccurate)
    When this option is selected, JetBrains Rider uses tabs for both indents and alignment, which may not result in precise alignment:

    general formatter style2

  • Mix tabs and spaces for optimal fill
    When this option is selected, JetBrains Rider uses tabs for both indents and alignment adds necessary spaces for precise alignment.

    general formatter style3

Naming

On this tab, you can configure symbol naming rules for C#.

JetBrains Rider helps you define, control, and apply naming style for symbols in your code. Naming style is implemented as a set of rules, each of which targets specific identifiers with the set of constraints (e.g. a rule can target static private readonly fields). Each rule can have one or more associated styles that define suffixes, prefixes, capitalization of compound words, etc.

These rules are taken into account when JetBrains Rider produces new code with code completion and code generation features, applies code templates and performs refactorings. JetBrains Rider also helps you detect and fix violations of naming rules. If necessary, the automatic checkup of naming rules can be configured or disabled.

Code Style

Preferences configurable on this tab are taken into account when JetBrains Rider produces new code with code completion and code generation features, applies code templates and performs refactorings. They can also be applied to the existing code by using code cleanup with the corresponding settings.

The preferences with the Notify with selector have corresponding code inspections that notify you if this aspect of the code style in the inspected scope differs from the preferred style. Using the selectors, you can configure severity levels of the inspections.

Item

Description

'var' usage in declarations

Preferences in this section define how the implicitly typed local variables (also known as var keyword) should be used. You can set different preferences of using 'var' or explicit type for different types:
  • For built-in types — applies to C# built-in types.

  • For simple types — applies to types without generic parameters.

  • Elsewhere — applies to generic types and deconstruction declarations.

For each of these preferences you can opt for using 'var', explicit type, or 'var' when evident, which means that 'var' should be only used for variables initialized as creation of objects, arrays, and literals or explicit casts. For more information, see Code Syntax Style: Implicit/Explicit Typing ('var' Keyword).

Prefer separate declarations for deconstructed variables

By default, JetBrains Rider will suggest joined notation for multiple var's in deconstruction declarations, e.g. var (x, y) = GetTuple();. You can select Prefer separate declarations for deconstructed variables to opt for separate notation, e.g. (var x, var y) = GetTuple();.

Use 'var' keyword for discards

By default, JetBrains Rider suggests using standalone _ for discards, e.g. (_, _) = (0, 1);. You can select Use 'var' keyword for discards to always use var with discards where appropriate thus making sure that there are no conflicts with variables in the scope that may be named _.

Instance members qualification

Preferences in this section define how to use 'this' qualifier. For more information, see Code Syntax Style: Optional Member Qualifiers.

Static members qualification

Preferences in this section define how to qualify static members. For more information, see Code Syntax Style: Optional Member Qualifiers.

Built-in type naming

Preferences in this section define how to reference C# built-in types: you can either use C# keywords or CLR type names. For more information, see Code Syntax Style: Built-In Type References.

Reference qualification

Preferences in this section define the style of namespace imports:
  • Prefer fully qualified references - select this check box if you prefer use fully qualified names rather than namespace import directives for imported types.

  • Add 'using' directive to the deepest scope - if this check box is selected, namespace import directives are added inside the namespace where the imported types are used; otherwise the import directives are added in the top of the file.

  • Prefer fully qualified using name at nested scope - if this check box is selected, fully qualified names for imported types are preferred in nested types and namespaces.

  • Allow using alias directive - selecting this check box allows using aliases in namespace import directives, as opposed to fully qualified namespace names.

  • Allow the 'global::' prefix - if this check box is selected, the global:: prefix are not removed, as in global::System.String. For more information, see How to: Use the Global Namespace Alias.

A number of other options related to namespace imports can be configured on the Code Editing | C# | Namespace Imports page of JetBrains Rider options.

Modifiers

Preferences in this section define how to arrange modifiers of types and members. For more information, see Code Syntax Style: Modifiers.

Arguments

Preferences in this section let you define how named or positional arguments should be enforced for specific types of parameters. For more information, see Code Syntax Style: Named/Positional Arguments.

Parentheses

Preferences in this section let you define when optional parentheses should be removed or added if they help you clarify precedence of operations. For more information, see Code Syntax Style: Optional Parentheses.

Braces

Preferences in this section let you define which statements require braces for single nested statements. For more information, see Code Syntax Style: Braces for Single Nested Statements.

Code body

Preferences in this section let you define which kinds of members should be declared with the expression body and which with the block body. For more information, see Code Syntax Style: Bodies of Function Members.

Attributes

Preference in this section defines how to arrange multiple attributes. For more information, see Code Syntax Style: Multiple Attributes.

Braces Layout

Use this tab to adjust the way JetBrains Rider arranges braces when it generates and reformats your code; in particular, there are several ways to position braces after if or for operators.

For every item, there is a preview pane in the bottom part of the page where you can observe changes after tweaking specific preferences.

Blank Lines

This tab lets you configure whether JetBrains Rider should increase or decrease the number of blank lines around namespaces, members, regions and groups of import directives. You can adjust the values and check the preview pane at the bottom of the page to see how your preferences affect the code.

Options in the Preserve Existing Formatting section are only applied when JetBrains Rider reformats existing code whereas the Blank Lines section contains options that also take effect when you type new code.

Line Breaks and Wrapping

Use this tab to configure how JetBrains Rider should add or remove line breaks before/after specific language constructs, and whether to wrap long lines exceeding the length specified by the Right margin (columns). You can adjust the values and check the preview pane at the bottom of the page to see how your preferences affect the code.

Note that preferences with names starting with Keep existing... allow you to keep existing formatting for other preferences in the same group.

Spaces

This tab is used to configure automatic insertion/deletion of spaces in certain positions of your source code. You can adjust the values and check the preview pane at the bottom of the page to see how your preferences affect the code.

Null Checking

Use this tab to customize generating null-checking routines for exceptions and assertions.

Null checks for exceptions and assertions

There are situations when encountering an object that is a null reference is critical in your program and should be either logged or signalled by throwing an exception. A typical example here is throwing an ArgumentNullException in a function that is not designed to accept null objects.

Generating null checks for exceptions and assertions

You can generate these kinds of null checks in the following ways:

  • Press Alt+Enter on a parameter or expression and choose the corresponding context action:

    JetBrains Rider: Checking parameter for null

  • If a parameter is marked with the [NotNull] attribute, you can set the caret directly after the parameter name or parameter type and press !:

    private void Foo([NotNull] object/*!*/ arg/*!*/)

  • When generating a constructor (Alt+Insert | Constructor), select Check parameters for nul in the dialog.

  • To generate an assertion for null for any nullable expression, JetBrains Rider provides the Assert expression for null action on Alt+Enter. Depending on the nullability analysis settings, it appears as a quick-fix or a context action.

    JetBrains Rider: Asserting expression for null
    This action becomes unavailable if JetBrains Rider infers that the expression can never be null.

If you use code annotations in your project, JetBrains Rider will decorate parameters, which you check for null, with the [NotNull] attribute. This will let JetBrains Rider notify you when null objects are passed to the decorated parameters.
You can disable adding [NotNull] by clearing the Automatically propagate annotations check box on the Editor | Inspection Settings | Code Annotations page of JetBrains Rider settings (Ctrl+Alt+S).

Configuring null checks for exceptions and assertions

This kind of null checks can be written in different ways, so they are configurable on the Editor | Code Style | C# | Null checking page of JetBrains Rider settings (Ctrl+Alt+S), which is also accessible from the Alt+Enter menu on the corresponding actions:

JetBrains Rider: a shortcut for configuring null-check pattern

This settings page lists all predefined null-checking patterns in the priority order with higher priority patterns shown on the top. When JetBrains Rider generates a null check, it will take the highest-priority pattern which semantically suits the context, taking into account the current C# version. By default, JetBrains Rider automatically detects C# version based on the associated compiler. However, you can specify the target C# version explicitly — press Alt+Enter on the project in the Solution Explorer and use the Language level selector on the Application page of the Project Properties dialog.

For example, with the default configuration the 'throw expression' pattern ($EXPR$ ?? new System.ArgumentNullException($NAME$);) has higher priority than the 'classic' throw statement (if ($EXPR$ == null) throw new System.ArgumentNullException($NAME$);). But if expressions are not allowed in the current context, JetBrains Rider will skip the first and use the second pattern:

// Press Alt+Enter on 'arg' and choose 'Check parameter for null' private void Foo(object arg) { // as expression is not allowed here, the statement is generated: if (arg == null) throw new ArgumentNullException(nameof(arg)); } // However, in the following case the expression is already there private object myField; private void Foo(object arg) { myField = arg; } // so JetBrains Rider will use the first pattern: private object myField; private void Foo(object arg) { myField = arg ?? throw new ArgumentNullException(nameof(arg)); }

The same applies to generating assertions: JetBrains Rider will use the first pattern that is marked with Can use for assertion.

If you have any preferences for generating null checks, use the Move up (Alt+U)/ Move down (Alt+D) buttons on the settings page to raise the priority of patterns that you prefer.

Creating custom null checks for exceptions and assertions

If your codebase provides dedicated helper methods for handling null checks, you may want to create your own null-checking patterns by editing the two custom patterns that are highlighted in bold on the settings page — Custom (statement) and Custom (expression):

JetBrains Rider: custom patterns for null checking

By default, these two patterns have the lowest priority, meaning that they will never be used for generation. So if you are going to use them, move them up to raise their priority.

When a custom pattern is selected in the list, you can edit it in the text field on the bottom of the page using the $EXPR$, $NAME$, $MESSAGE$ placeholders. As long as the pattern is valid, JetBrains Rider will display the corresponding icon below the text field.
You can also tick the Can use for assertion check box to make the pattern work with the Assert expression for null action.

Other

This tab helps configure a number of additional settings that control how JetBrains Rider treats new code and reformats existing code. You can adjust the values and check the preview pane at the bottom of the page to see how your preferences affect the code.

Last modified: 21 December 2018