ReSharper 2020.2 Help

Invert Boolean refactoring

ReSharper | Refactor | Invert Boolean…
ReSharper_InvertBool

This refactoring allows you to automatically modify a boolean member so that its value is inverted. If you apply this refactoring to a boolean field or a property, ReSharper lets you specify a new name and invert all its usages, that is if you refactor private bool isEnabled; to private bool isDisabled;, ReSharper will make sure that, for example, if(isEnabled) becomes if(!isDisabled) everywhere.
If you apply this refactoring to a boolean method, ReSharper lets you choose whether to invert its internal logic, usages, or both.

In the example below, we use this refactoring to invert the IsEven method. We specify a new name IsOdd and choose to invert both the internal logic and usages:

public bool IsEven(int input) { return input % 2 == 0; } public void Test(int value) { if (IsEven(value)) Console.WriteLine("\n the number is even"); }
public bool IsOdd(int input) { return input % 2 != 0; } public void Test(int value) { if (!IsOdd(value)) Console.WriteLine("\n the number is even"); }

Invert a boolean method or property

  1. Place the caret at the declaration or a usage of a boolean member in the editor, or select it in the File Structure window.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Invert Boolean

    • Right-click and choose Refactor | Invert Boolean in the context menu.

    • Choose ReSharper | Refactor | Invert Boolean… in the main menu.

  3. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

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
Feature is available in C#Feature is not available in Visual Basic .NETFeature is not available in C++Feature is not available in HTMLFeature is not available in ASP.NETFeature is not available in RazorFeature is not available in JavaScriptFeature is not available in TypeScriptFeature is not available in CSSFeature is not available in XMLFeature is not available in XAMLFeature is not available in Resource filesFeature is not available in build script filesFeature is not available in ProtobufFeature is not available in JSON
Last modified: 03 September 2020