| title | Conditional and logic refactorings | ||
|---|---|---|---|
| description | Learn how to use Quick Actions and Refactorings to convert, invert, split, merge, and simplify conditional statements and logic operators in Visual Studio. | ||
| ms.date | 04/03/2026 | ||
| ms.topic | reference | ||
| ms.custom | awp-ai | ||
| author | mikadumont | ||
| ms.author | midumont | ||
| ms.subservice | general-ide | ||
| dev_langs |
|
Visual Studio provides several refactorings for working with conditional statements and logic operators. You can access these refactorings through the Quick Actions and Refactorings menu (Ctrl+.).
Applies to: C#
This refactoring converts an if statement to a switch statement or a C# 8.0 switch expression, and vice versa. It's useful when an if statement would be clearer as a switch.
-
Place your cursor in the
ifkeyword. -
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
-
Select one of the following options:
Applies to: C#
This refactoring converts a switch statement to a C# 8.0 switch expression, and vice versa. If you're only using expressions, switch expressions provide a more concise syntax.
-
Place your cursor in the
switchkeyword. -
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
-
Select Convert switch statement to expression.
Applies to: C#, Visual Basic
This refactoring inverts an if or if else statement without changing the meaning of the code. It's useful when the inverted form is easier to understand.
-
Place your cursor in an
iforif elsestatement. -
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
-
Select Invert if.
Applies to: C#, Visual Basic
This refactoring inverts a conditional expression or a conditional AND/OR operator. It's useful when the inverted form is easier to understand, and avoids errors from doing the inversion by hand.
-
Place your cursor in a conditional expression or a conditional AND/OR operator.
-
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
-
Select Invert conditional or Replace '&&' with '||'.
Applies to: C#, Visual Basic
This refactoring splits an if statement that uses && or || operators into a nested if statement, or merges an inner if statement with an outer if statement.
-
Place your cursor in the
ifstatement by the&&or||operator. -
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
-
Select Split into nested if statements.
-
Place your cursor in the inner
ifkeyword. -
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
-
Select Merge with outer if statement.
Applies to: C#
This refactoring simplifies a conditional expression by removing unnecessary code, providing more clarity and concise syntax.
-
Place your cursor on the conditional expression.
-
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
-
Select Simplify conditional expression.












