Convert to String Interpolation

Purpose

Converts a formatted string into string interpolation. Interpolated strings are much easier to read than formatted strings.

Availability

Available when the cursor is on a composed string expression (as shown in the code below) or on the call of one of the following methods, assuming the method argument contains formatted string.

  • string.Format
  • Debug.Print
  • Console.Write
  • Console.WriteLine
  • StringBuilder.AppendFormat

Usage

  1. Place the caret on a composed string expression.

    Note

    The blinking cursor shows the caret's position at which the Refactoring is available.

    return "Found record at the position " + i;
    
  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Convert to string interpolation from the menu.

After execution, Refactoring replaces the composed string to the interpolated string.

return $"Found record at the position {i}";
Note

This feature is available as a part of Code Cleanup.

See Also