Code Style. C#
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
- Code Style
- Braces Layout
- Blank Lines
- Line Breaks and Wrapping
- Null Checking
- XML Documentation
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.
By default, JetBrains Rider uses Visual Studio preferences for tabs and indents (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.and ). However, if you prefer to share formatter preferences with your team using
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:
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:
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:
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:
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.
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.
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.
|'var' usage in declarations|| Preferences in this section define how the implicitly typed local variables (also known as |
|Prefer separate declarations for deconstructed variables|
By default, JetBrains Rider will suggest joined notation for multiple
|Use 'var' keyword for discards|
By default, JetBrains Rider suggests using standalone
|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|
|Reference qualification and 'using' directives|| |
Preferences in this section define the style of namespace imports:
A number of other options related to namespace imports can be configured on thepage of JetBrains Rider options.
Preferences in this section define how to arrange modifiers of types and members. For more information, see Code Syntax Style: Modifiers.
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.
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.
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.
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.
Preference in this section defines how to arrange multiple attributes. For more information, see Code Syntax Style: Multiple Attributes.
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
For every item, there is a preview pane in the bottom part of the page where you can observe changes after tweaking specific preferences.
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.
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.
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
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:
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 ), select Check parameters for nul in the dialog.
To generate an assertion for
nullfor 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. This action becomes unavailable if JetBrains Rider infers that the expression can never be
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 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 thepage of JetBrains Rider settings (Ctrl+Alt+S), which is also accessible from the Alt+Enter menu on the corresponding actions:
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 version 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:
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):
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
$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.
This tab helps you configure a number of settings that control how JetBrains Rider generates new XML Doc Comments code and how it 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.
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.