ReSharper 2022.2 Help

Syntax Style

ReSharper options: Code Editing | C# | Syntax Style

Preferences configurable on this page help you enforce code syntax style — how to use interchangeable language syntax constructions. These preferences are taken into account when ReSharper 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 syntax style in the inspected scope differs from the preferred style. Using the selectors, you can configure severity levels of the inspections.

Apply syntax style on completion

Use this checkbox to apply configured syntax styles when you accept code completion suggestions.

'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.

For more information, see Code Syntax Style: Implicit/Explicit Typing ('var' Keyword).

Prefer Roslyn (Visual Studio) logic for type evidence

When configuring preferences of using 'var' keyword vs. explicit type, you can opt for Use 'var' when evident. This option seems self-explanatory but in some cases it might be unclear what is considered 'evident' and what is not.

Apart from that, there are some differences between what is considered evident (apparent) by ReSharper and by Visual Studio when both products suggest using 'var' keyword or explicit type.

Use this checkbox to apply Visual Studio logic for suggesting 'var' or explicit type when Use 'var' when evident is chosen in any options above.

To learn the differences between ReSharper and Visual Studio logic, see Use 'var' when evident: what is considered evident?.

This checkbox is enabled automatically when the csharp_style_var_when_type_is_apparent property in EditorConfig is set to true and has some severity level. For example: csharp_style_var_when_type_is_apparent = true:warning.

Prefer separate declarations for deconstructed variables

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

Use 'var' keyword for discards

By default, ReSharper suggests using standalone _ for discards, for example (_, _) = (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 types

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 and 'using' directives

Preferences in this section define the style of namespace imports:

  • Prefer fully qualified references — select this checkbox if you prefer using fully qualified names rather than namespace import directives for imported types.

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

  • Place 'System.*' and 'Windows.*' namespaces first when sorting 'using' directives — if this checkbox is selected, 'using' directives like using System.Linq; will be placed above other using directives when sorting. Otherwise, these directives will be sorted alphabetically with others.

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

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

  • Allow the 'global::' prefix — if this checkbox 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 ReSharper 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 Configure preferences for expression-bodied functions.

By default, ReSharper applies heuristics to decide whether to convert block bodies of methods to expression bodies. Among other things, this means that ReSharper will not suggest expression bodies for void methods and for methods that contain a lambda expression.

You can simplify this logic by clearing the Apply style heuristics checkbox. In this case, the logic is simple: if you choose to use expression body for some kind of members, ReSharper will suggest expression body if the declaration of a member of this kind consists of a single statement.

The Namespaces option lets you choose whether to use file-scoped namespaces in files containing a single namespace declaration.

Attributes

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

Trailing Comma

Preferences in this section define how to treat trailing commas in declarations with multiple items and similar constructs (object, array, and collection initializers, as well as enums and switch expressions) . For more information, see Code Syntax Style: Trailing Commas.

Object Creation

Preferences in this section define whether to add optional type specification when creating new objects with the new operator. For more information, see Code Syntax Style: Object Creation ('new()' vs 'new T()').

Default Value

Preferences in this section define whether to use optional type specification (T) in the default expressions. For more information, see Code Syntax Style: Default Value Expressions ('default' vs 'default(T)').

If there are .editorconfig files that affect your solution, preferences on this page 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 style options overridden by EditorConfig styles

Changing code syntax styles from the editor

You can also change specific syntax style preferences right in the editor, by pressing Alt+Enter where style violations are highlighted. For example, you can change the preference of 'var' usage in declarations for simple types:

Changing code style preference for 'var' keyword

... as well as the severity level of the corresponding inspection:

Changing inspection severity level

Note that when you change preferences from the editor, your changes are saved using the smart save logic.

Last modified: 29 December 2021