Edit

Share via


Expression and pattern refactorings

Visual Studio provides several refactorings for modernizing C# syntax with newer language features. You can access these refactorings through the Quick Actions and Refactorings menu (Ctrl+.).

Use new()

Applies to: C#

This refactoring uses new() (target-typed new expressions) so you don't have to write repetitive code by repeating the type twice. It's useful when you can't use var or have a code style preference to not use var.

  1. Place your caret on the field declaration.

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

  3. Select Use 'new(…)':

    Screenshot of Use 'new(...)'.

Use pattern matching

Applies to: C#

This refactoring applies C# pattern matching capabilities to make code more clear and concise.

  1. Place your caret inside the statement.

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

  3. Select Use pattern matching.

    Screenshot of Make class abstract.

Use expression body or block body for lambda expressions

Applies to: C#

This refactoring converts a lambda expression to use either an expression body or a block body, according to your preference.

  1. Place your cursor on the right of a lambda operator.
  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

Screenshot of Use lambda expression/block body.

  1. Select Use block body for lambda expressions or Use expression body for lambda expressions.