Edit

Share via


Type, string, and syntax conversion refactorings

Visual Studio provides several refactorings for converting between types, string formats, and syntax styles. You can access these refactorings through the Quick Actions and Refactorings menu (Ctrl+.).

Convert anonymous type to class

Applies to: C#, Visual Basic

This refactoring converts an anonymous type to a named class, useful when your code grows and you want to continue building on the type.

  1. Place your cursor in an anonymous type.

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

    Screenshot of Convert Anonymous Type to Class.

  3. Press Enter to accept the refactoring.

    Screenshot of Convert Anonymous Type to Class accepted.

Convert anonymous type to tuple

Applies to: C#, Visual Basic

This refactoring converts an anonymous type to a tuple for more lightweight syntax.

  1. Place your cursor in an anonymous type.

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

    Screenshot of Convert Anonymous Type to Tuple.

  3. Press Enter to accept the refactoring.

    Screenshot of Convert Anonymous Type to Tuple accepted.

Convert between regular string and verbatim string literals

Applies to: C#

This refactoring converts between regular string and verbatim string literals. Converting to a regular string can save space, while converting to a verbatim string can provide more clarity.

  1. Place your caret on either the regular string or verbatim string literal.

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

  3. Select one of the following options:

    Select Convert to regular string.

    Screenshot of Convert to regular string.

    Select Convert to verbatim string.

    Screenshot of Convert to verbatim string.

Convert typeof to nameof

Applies to: C#, Visual Basic

This refactoring converts typeof(<QualifiedType>).Name to nameof(<QualifiedType>) in C# and GetType(<QualifiedType>).Name to NameOf(<QualifiedType>) in Visual Basic. Using nameof avoids the reflection involved with retrieving a type object.

  1. Place your cursor within the typeof(<QualifiedType>).Name instance for C# or the GetType(<QualifiedType>).Name in Visual Basic.

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

  3. Select from one of the following options:

    • C#
      Select Convert 'typeof' to 'nameof': Screenshot of the Quick Actions and Refactorings menu in Visual Studio with Convert 'typeof' to 'nameof' selected, and C# code changes shown.

    • Visual Basic
      Select Convert 'GetType' to 'NameOf': Screenshot of the Quick Actions and Refactorings menu in Visual Studio with Convert 'GetType' to 'NameOf' selected and Visual Basic code changes shown.

Use explicit type instead of var

Applies to: C#

This refactoring replaces var in a local variable declaration with an explicit type, improving readability. However, var must be used when a variable is initialized with an anonymous type and the properties of the object are accessed at a later point. For more information, see Implicitly typed local variables (C#).

  1. Place the caret on the var keyword.

  2. Press Ctrl+. or click the screwdriver screwdriver icon icon in the margin of the code file.

    Screenshot of Use explicit type quick actions menu.

  3. Select Use explicit type. Or, select Preview changes to open the Preview Changes dialog, and then select Apply.

Simplify string interpolation

Applies to: C#, Visual Basic

This refactoring simplifies a string interpolation expression for more clarity and concise syntax.

  1. Place your caret on the string interpolation.

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

  3. Select Simplify interpolation.

    Screenshot of Simplify string interpolation.