ReSharper
 
Get ReSharper
You are viewing the documentation for an earlier version of ReSharper.

Surround code fragments with templates

Last modified: 21 July 2022

ReSharper | Edit | Surround With Template…

Ctrl+E U (ReSharper_SurroundWith)

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 at the end so that you could continue typing from a handy position.

Consider the following code fragment:

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

ReSharper: Surrounding statments with 'using'

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 popup 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:

ReSharper: Surrounding statments with 'using'

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:

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.