Syntax styles
One of the aspects of the code style is how to use interchangeable language syntax constructions. For example, two method definitions below are identical from the compiler point of view, but the choice of syntax constructions makes them look different:
[Conditional("DEBUG")]
[Test]
static public void Foo(String str)
{
foreach (Char ch in str)
{
System.Console.WriteLine(value: ch);
}
}
|
[Test, Conditional("DEBUG")]
public static void Foo(string str)
{
foreach (var ch in str)
Console.WriteLine(ch);
}
|
Configure code syntax styles
When you start using ReSharper, it will refer to its default settings to make sure that interchangeable syntax constructions are used consistently throughout your codebase. However, you can configure each individual preference to your liking. There are two ways to configure code syntax styles:
Configure your preferences on the Alt+R, O). These modifications will be saved using the mechanism of layered settings so that you can apply them either on local machine for all your projects, or share them with your teammates.
page of ReSharper options (Use ReSharper custom EditorConfig code style properties to specify your preferences in EditorConfig files, which will also allow you to have consistent code styles across your team.
Note that preferences specified in .editorconfig files will override preferences saved in ReSharper settings.
Check and fix code syntax style with code inspections
ReSharper provides a number of code inspections that check your code for compliance with your code syntax style and suggests quick-fixes where the style is violated — for example, if the order of modifiers differs from your preferences:
If you need to review all code syntax style problems in a desired scope, up to the whole solution, you can run code inspection in this scope. When the analysis is over, group the results by issue category, and check what problems there are and where:
All of the code syntax style inspections are configurable and you can disable the ones that you do not like.
You can also go through the list of code syntax style inspections and configure all of them in one go. There are two ways to do so:
On the Alt+R, O), use selectors in the right column and choose Do not show for inspections that you want to disable.
page of ReSharper options (On this options page, you can also change preferences for each code syntax style rule.
On the Alt+R, O), find code syntax style inspections in the groups and clear check boxes next to the ones you want to disable.
page of ReSharper options (
Apply code syntax style with code cleanup
To apply code syntax style in the desired scope, you can also use code cleanup. You can either run code cleanup with one of the built-in profiles Full Cleanup or Reformat & Apply Syntax Style, or create and run a custom profile with a desired subset of code syntax style rules.
Additional actions for code syntax style in C# and C++
In C#, ReSharper provides a dedicated action Control+Alt+S (
) for applying your code syntax styles.Apply C# code syntax styles for existing code
Select the scope where you want to apply syntax style preferences:
Make a selection in the editor to apply syntax style preferences in the selection.
Set the caret anywhere in the file to apply syntax style preferences in the file.
Select one or more items in the Solution Explorer to apply syntax style preferences in files under these items and their child items.
Press Control+Alt+S or choose
from the main menu. Alternatively, you can press Control+Shift+A, start typing the command name in the popup, and then choose it there.
You can also apply code syntax styles in the current selection with the corresponding context action.
Apply C# code syntax styles in the current selection
In the editor, select a block of code where you want to apply code syntax styles.
Press Alt+Enter or click the action indicator to the left of the caret to open the action list.
Select Apply syntax style from the action list.
Applicability in different languages
Most of ReSharper's code syntax style features are available in C# and Visual Basic .NET, some are available in several languages, others are language-specific. The table below lists all code syntax style features and languages/technologies where they are supported.
The instructions and examples given in the topics within this section address the use of code syntax style features in C#. For more information on code syntax style features available for specific languages, see the corresponding topics in the ReSharper by language section.
Feature | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
| |||
|
|
|
|
|
|
|
|
|
|
|
|
|
| ||
|
|
|
|
|
|
|
|
|
|
|
|
|
| ||
|
|
|
|
|
|
|
|
|
|
|
|
| |||
|
|
|
|
|
|
|
|
|
|
|
|
| |||
|
|
|
|
|
|
|
|
|
| ||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
| ||
|
|
|
|
|
|
|
|
|
|
|
|
|
| ||
|
|
|
|
|
|
|
|
|
|
|
|
|
| ||
|
|
|
|
|
|
|
|
|
|
|
|
|
| ||
|
|
|
|
|
|
|
|
|
|
|
| ||||
|
|
|
|
|
|
|
|
|
|
|
|
|
| ||
|
|
|
|
|
|
|
|
|
|
|
|
|
| ||
|
|
|
|
|
|
|
|
|
|
|
|
|
|