Invert Boolean refactoring
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, JetBrains Rider lets you specify a new name and invert all its usages, that is if you refactor private bool isEnabled;
to private bool isDisabled;
, JetBrains Rider will make sure that, for example, if(isEnabled)
becomes if(!isDisabled)
everywhere.
If you apply this refactoring to a boolean method, JetBrains Rider 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:
Before refactoring
public bool IsEven(int input){ return input % 2 == 0;}public void Test(int value){ if (IsEven(value)) Console.WriteLine("\n the number is even");}
After refactoring
public bool IsOdd(int input){ return input % 2 != 0;}public void Test(int value){ if (!IsOdd(value)) Console.WriteLine("\n the number is even");}
Place the caret at the declaration or a usage of a boolean member in the editor, or select it in the Structure window window.
Do one of the following:
Press CtrlShift0R and then choose Invert Boolean.
Choose Refactor | Invert Boolean from the main menu.
If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
tip
Right after the refactoring has been completed, all changes that it makes anywhere, including other files, are registered as a single operation. So you can use the Undo action Ctrl0Z to roll back all these changes with a single keystroke.