ReSharper 2017.3 Help

Surrounding Code Fragments with Templates

ReSharper | Edit | Surround With Template…

Surround templates are used to quickly enclose an expression, a single statement, or an arbitrary block of code with if...else, try...catch or other code constructs. As the template expands, ReSharper intelligently reformats the code, adjusts the selection, and/or repositions the caret in the end so that you could continue typing from a handy position.

To apply a surround template

  1. In the editor, select a block of code that you want to surround with a template.
  2. Do one of the following:
    • As soon as the surround action indicator ThemedIcon SurroundTemplate Screen Gray appears to the left of the selected code block, press Alt+Enter or click on the indicator and choose the desired surround template in the action list:
      actions list surround
      Note that only templates with a specified mnemonic digit appear under Surround with....
    • Press Ctrl+Alt+J or choose ReSharper | Edit | Surround With Template‚Ķ in the main menu. In the Surround With pop-up that appears, choose one the desired template or start typing the template name to narrow down the list.
    • Just start typing the template name when the selection is made. Of course, the selection will disappear at first, but as soon as you choose the template in the completion list and press Enter, the selection surrounded with the template will reappear:

Consider the following code fragment:

var fileStream = new FileStream("file.txt", FileMode.Open);

We would like to enclose the statement in a using directive. ReSharper conveniently offers the corresponding predefined template to streamline this operation. Pressing Ctrl+Alt+J after selecting this code block opens a drop-down list with available templates:


To choose the using template, you can do one of the following:

  • Click it with the mouse.
  • Go down to the corresponding menu item using arrow keys and press Enter.
  • Start typing usi... when the pop-up is open.
  • Press a key corresponding to the template mnemonic digit, which is shown next to the template.

ReSharper will insert the template code and deploy a Hot Spot Session in the editor so that you could provide values for the parameters of the template:


We supply the desired disposable resource at the first parameter and press Tab or Enter to complete the hot spot session. The caret and selection are moved to the positions specified by the template designer, so that you can continue typing inside the created using statement.

This feature is supported in the following languages and technologies:

Language: C# Language: VB.NET Language: C++ Language: HTML Language: ASP.NET Language: Razor Language: JavaScript Language: TypeScript Language: CSS Language: XML Language: XAML Language: Resx Language: Build Scripts Language: Protobuf Language: JSON
Feature is available Feature is available Feature is available Feature is available Feature is available Feature is available Feature is available Feature is available Feature is available Feature is available Feature is available Feature is available Feature is available Feature is not available Feature is not available

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 22 February 2018

See Also