ReSharper 2020.1ヘルプ

ネーミングスタイル

ReSharperは、コード内のシンボルの命名スタイルを定義、制御、および適用できます。命名スタイルは、一連の制約として特定の識別子をターゲットとする一連のルールとして実装されます(たとえば、ルールは静的プライベート読み取り専用フィールドをターゲットにすることができます)。各ルールには、接尾辞、接頭辞、複合語の大文字の使用などを定義する1つ以上の関連付けられたスタイルを含めることができます。

これらのルールは、ReSharperがコード補完およびコード生成機能を使用して新しいコードを生成し、コードテンプレートを適用し、リファクタリングを実行するときに考慮されます。ReSharperは、命名規則の違反を検出して修正するのにも役立ちます。必要に応じて、命名規則の自動チェックを構成または無効にできます。

命名規則を構成する

すぐに利用できる、ReSharperは各タイプの識別子の命名規則を提供します。これらのルールは、マイクロソフトの命名ガイドライン(英語).NETプラットフォームのコーディングスタイル(英語)、およびさまざまなベストプラクティスに基づいています。
ReSharper命名規則のデフォルトと上記のガイドラインのいずれかとの間には厳密な対応はありません。

個人の好みや会社の標準がReSharperのデフォルトと異なる場合は、柔軟な方法で命名スタイルを設定できます。識別子の種類ごとに、大文字の規則、接頭辞と接尾辞、異なるアクセス権のバリエーション、保存する略語を選択できます。

命名スタイルの設定は、レイヤーベースの設定のメカニズムを使用して保存されます。とりわけ、このメカニズムにより、さまざまなソリューションのさまざまな設定を維持したり、これらの設定をVCSに保持したり、チームメンバーと自動的に共有することができます。

Modify a default naming rule

  1. Do one of the following:

    • Press Alt+Enter on a highlighted symbol with inconsistent naming, then choose Inspection 'Inconsistent Naming' | Change settings for naming rule '[rule name]'

    • In the main menu, choose ReSharper | Options | Code Editing | [Language] | Naming Style, select the desired rule, and click Edit ThemedIcon EditorOptionsPage Screen Gray png or just double-click it.

  2. In the Edit Rule Settings dialog that opens, check the existing style for the rule.

  3. If the existing style is acceptable, but you would like to allow other styles for this rule, click Add ThemedIcon AddedParameter Screen Gray png. When there are several styles for a single rule, ReSharper does not detect code style violation if a corresponding symbol name matches at least one of these styles. Otherwise ReSharper detects a code style violation and suggests renaming the symbol according to the default style (the first style in the list). To change the default style, use Move Up ThemedIcon Up Screen Gray png and Move Down ThemedIcon Down Screen Gray png buttons.

  4. To configure a specific style, select it in the list and configure prefix, suffix, and choose a naming style.

  5. To remove an unnecessary style, select it in the list and click Remove ThemedIcon Delete Screen Gray png.

  6. If necessary, you can disable detection of code style violations associated with this rule, allowing ReSharper to detect other code style violations. To do so, clear the Enable inspections checkbox.

  7. Click Set to close the Edit Rule Settings dialog.

  8. Click Save to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see Manage and Share ReSharper Settings.

If the existing rules do not cover all aspects of your naming convention, you can create custom rules. For example, you may want that enums had upper camel case capitalization and the 'E' prefix.

Create a custom naming rule

  1. In the main menu, choose ReSharper | Options | Code Editing | C# | Naming Style and click Advanced settings

  2. In the Advanced Naming Settings dialog that opens, click Add ThemedIcon AddedParameter Screen Gray png to create a custom naming rule.

    ReSharper: naming style options. advanced settings

  3. In the Edit Extended Naming Rule dialog that appears, configure the new rule: specify a name for the new rule in the Rule Description field, select the target identifiers in the Affected entities area, use Access rights and Static/non-static areas to further constrain the scope of the rule.

  4. In the Naming style section, configure one or more styles for the rule as described in the procedure above.

    ReSharper: Naming style options. Edit extended naming rule

  5. Click OK to close the Edit Extended Naming Rule dialog, and then again OK to close the Advanced Naming Settings dialog.

  6. Click Save to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see Manage and Share ReSharper Settings.

Use EditorConfig to configure naming rules

If you use EditorConfig to maintain code styles for your project, you can also configure naming rules from .editorconfig files.

To configure naming rules from EditorConfig, you have to select the Read settings from editorconfig and project settings checkbox on the Code Inspection | Settings page of ReSharper options (Alt+R O).

You can also export naming styles that you already configured in ReSharper settings to an .editorconfig file.

Naming style settings in .editorconfig files are configured using the properties of .NET naming conventions for EditorConfig(英語).

As EditorConfig convention suggests, ReSharper will apply naming rules defined in files named .editorconfig in the directory of the current file and in all its parent directories until it reaches the root filepath or finds an EditorConfig file with root=true. File masks specified in .editorconfig files, for example *Test.cs are also taken into account.

Ignore uppercase abbreviations that conflict with a CamelCase naming style

Sometimes you may want to use uppercase abbreviations (acronyms) in CamelCase symbol names. By default, ReSharper detects it as inconsistent camel casing and reports a problem:

ReSharper: Nming style. abbreviations

In this case, you can configure the list of abbreviations to be ignored by the naming style inspection.

It is important to note that uppercase abbreviations should not contradict the naming style defined for specific kind of identifier.
For example, if you added MS to the ignored abbreviations, MSBuilder will be an acceptable name for identifiers with the UpperCamelCase style, but not for identifiers with lowerCamelCase or all_lower naming styles.
Similarly, myMSBuilder will be OK for lowerCamelCase- but not for UpperCamelCase-styled identifiers.

Add an abbreviation to the ignore list

  1. In the editor, set the caret at a highlighted symbol that uses the abbreviation in its name.

  2. Press Alt+Enter or click the action indicator to the left of the caret to open the action list.

  3. Choose Inspection 'Inconsistent Naming' | Add [abbreviation] to the abbreviations list

    Adding an abbreviation to the ignore list

    if necessary, you can select where to save this preference. For more information, see Manage and Share ReSharper Settings

If you have many abbreviations that could be used in symbol names, you can edit the list of ignored abbreviations.

Edit the list of ignored abbreviations

  1. In the main menu, choose ReSharper | Options | Code Editing | C# | Naming Style and click Advanced settings

  2. Add abbreviations that ReSharper should ignore to the Abbreviations field using spaces as delimiters.

  3. Click OK to close the Advanced Naming Settings dialog.

  4. Click Save to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see Manage and Share ReSharper Settings.

Detect and fix violations of naming rules

ReSharper uses the 'Inconsistent Naming' code inspection to detect violations of naming rules in your code. By default, this inspection is always applied during the design-time code inspection and highlights the detected violations as warnings in the editor. For example, according to the default style, names of interfaces should have 'I' prefix. If an interface name does not match this rule, ReSharper highlights this name and informs you about the problem when you hover the mouse over it:

ReSharper: Highlighted violation of the naming style

If you want to find naming style violations beyond the current file, you can run the 'Inconsistent Naming' inspection in the project or in the entire solution.

Find naming style violations in a specific scope

  1. In the editor, set the caret at a highlighted symbol that has inconsistent naming.

  2. Press Alt+Enter or click the action indicator to the left of the caret to open the action list.

  3. Choose Inspection 'Inconsistent Naming' | Find similar issues in file and then select the desired scope.

    ReSharper: Using action list to find naming style issues

  4. All found naming style violations will be displayed in the Inspection Results window.

    ReSharper: Naming style violations in solution

  5. You can double-click the detected problems to navigate to the code of the corresponding symbols.

For most of the naming style violations highlighted in the editor, ReSharper suggests a quick-fix with a conforming name. You can press Alt+Enter and choose Rename to [conforming name] in the action list.

Quick-fix for naming style violation

Automatic correction of naming style violations can also be performed in the current file, project or solution using the fix in scope feature.

However, if the symbol with the calculated conforming name already exists in the same namespace, the quick-fix is not suggested. You can fix the naming of such symbol with the Rename refactoring.

Configure and disable automatic naming rules checkup

If you do not want ReSharper to check naming style in the existing codebase, you can either disable the code inspection that is responsible for it, or disable the automatic checkup of a specific rule. However, even if the automatic checkup is disabled, all naming rules will be respected when ReSharper helps you produce new code with code completion, generation, refactorings, and templates.

Disable automatic checkup of naming style

  • Press Alt+Enter on a highlighted symbol with inconsistent naming, then choose Inspection 'Inconsistent Naming' | Configure inspection severity | Do not show

  • On the Code Inspection | Inspection Severity page of ReSharper options (Alt+R O), start typing 'Inconsistent Naming', and then clear the checkbox next to the corresponding code inspection.

Disable automatic checkup of a specific naming rule

  1. Do one of the following:

    • Press Alt+Enter on a highlighted symbol with inconsistent naming, then choose Inspection 'Inconsistent Naming' | Change settings for naming rule '[rule name]'

    • In the main menu, choose ReSharper | Options | Code Editing | [Language] | Naming Style, select the desired rule, and click Edit ThemedIcon EditorOptionsPage Screen Gray png or just double-click it.

  2. In the Edit Rule Settings dialog that opens, clear the Enable inspections checkbox.

  3. Click Set to close the Edit Rule Settings dialog.

  4. Click Save to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see Manage and Share ReSharper Settings.

Use C# naming rules from existing code

If you check out a solution from a public repo or temporarily join a team where a different code naming style is established, you will probably want to follow naming rules in this solution but in ths same time not to change your globally configured naming rules.

ReSharper will handle such situations automatically — when you open a new solution where symbol naming differs from your global settings, it will detect the naming rules that diverge from your global settings, modify these rules accordingly, and then save the modified rules to the 'Solution personal' settings layer (.sln.DotSettings.user file next to the solution).

If this happens when you open a new solution, ReSharper will show a notification in the editor:

ReSharper: Naming rules auto-detection notification

You can click Configure to open the corresponding settings page (Code Editing | C# | Naming Style). On this settings page, the rules that were changed based on the solution code are highlighted with yellow. If you want to use ReSharper's default naming rules or configure naming style in your own way, clear the Use auto-detected rules checkbox.

ReSharper highlighting automatically-set naming rules with yellow

Applicability in different languages

This feature is supported in the following languages and technologies:

Language: C#Language: VB.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON

C#の機能の使用方法については、こちらの説明と例を参照してください。他言語の詳細については、言語によるReSharperの対応するトピックを参照してください。

最終更新日: 2020年8月05日

関連ページ:

コード補完 (IntelliSense)

ReSharperは、高度な機能を備えたVisual Studioのネイティブコード補完(IntelliSense)を補完し、拡張します。例:あなたの入力に基づいて提案のリストを絞り込み、選択したタイプと拡張メソッドを自動的にインポートし、メソッド名を補完するときに括弧を追加し、タイプに応じて変数と...

コード生成

ReSharperは、定型コードを生成するさまざまな方法を提供します。例:宣言されていないコードシンボルを使用し、これらのシンボルを用途に基づいて自動的に生成したり、型メンバーを生成したりすることができます。現在のファイルで設計時コードインスペクションが有効になっている場合、ReSharperは足り...

ReSharper設定の管理と共有

ReSharperは、階層化された設定のメカニズムを使用して環境設定を保存するため、次の利点があります。さまざまなソリューションで異なる設定を行うことができます。環境設定のさまざまなサブセットを別の場所に保存し、特定のソリューションで作業するときに環境設定を組み合わせることができます。例:記号のアイ...

EditorConfigを使用する

どのフォーマットスタイルの設定がファイルフォーマット情報ウィンドウ(ReSharper | Windows | ファイルフォーマット情報)の現在のファイルに影響するかを見ることができます。ReSharperは、EditorConfig形式で定義されたコード形式スタイル、コード構文スタイル、C#の命名...

アクションインジケータとアクションリスト

ReSharperがVisual Studioテキストエディターに追加する最も注目すべきものには、アクションインジケータとアクションリストがあります。これらは、ReSharperでサポートされるすべてのファイル(C#、VB.NET、XML、CSS、JavaScript、ASP.NETなど)に表示され...

設計時にコードの課題を検出する

ReSharper starts analyzing a code file as soon as you open it in the editor, all the way you edit it, until it is closed. All detected code issues are...