Edit

Share via


Conditional and logic refactorings

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+.).

Convert if statement to switch statement or switch expression

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.

  1. Place your cursor in the if keyword.

  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

  3. Select one of the following options:

    • Select Convert to 'switch' statement.

      Screenshot of Convert if statement to switch statement.

    • Select Convert to 'switch' expression.

      Screenshot of Convert if statement to switch expression.

Convert switch statement to switch expression

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.

  1. Place your cursor in the switch keyword.

  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

  3. Select Convert switch statement to expression.

    Screenshot of Convert switch statement to switch expression.

Invert if statement

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.

  1. Place your cursor in an if or if else statement.

    Screenshot of Invert if else.

  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

    Screenshot of Invert if else code fix.

  3. Select Invert if.

    Screenshot of Invert if else result.

Invert conditional expressions and conditional AND/OR operators

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.

  1. Place your cursor in a conditional expression or a conditional AND/OR operator.

  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

  3. Select Invert conditional or Replace '&&' with '||'.

    Screenshot of the Invert conditional option.

    Screenshot of the Replace && with || option.

Split or merge if statements

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.

Split an if statement

  1. Place your cursor in the if statement by the && or || operator.

  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

    Screenshot of Split If Statement.

  3. Select Split into nested if statements.

    Screenshot of Split If Statement Complete.

Merge an if statement

  1. Place your cursor in the inner if keyword.

  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

    Screenshot of Merge If Statement

  3. Select Merge with outer if statement.

    Screenshot of Merge If Statement Complete.

Simplify conditional expression

Applies to: C#

This refactoring simplifies a conditional expression by removing unnecessary code, providing more clarity and concise syntax.

  1. Place your cursor on the conditional expression.

  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

  3. Select Simplify conditional expression.

    Screenshot of Simplify conditional expression.