ReSharper Help  

Using 'Surround With' Templates

'Surround With' Templates are used to quickly enclose an expression, a single statement or an arbitrary block of code with a predefined C# code construction.  ReSharper intelligently reformats the code and substitutes template parameters (variables) with values or suggestion lists depending on local context. It adjusts the selection and repositions the caret as specified by template designer.

'Surround With' Templates are very similar to Live Templates. The main difference is that an existing piece of code is used as part of a 'Surround With' template.

To use a 'Surround With' template

  1. In the code editor, highlight the piece of code that you want to surround.
  2. Press Ctrl + Alt + J or select ReSharper | Code | Surround with. The Quick access list of available templates appears.
  3. Select a template from the list. Use Up and Down keys, the mouse, or press the key with the symbol shown before the name of the template. For a complete list of all available templates, select More. Then select the desired template using the provided Choose Template dialog:

    Tip   In this dialog, you can add any 'Surround With' Template to the Quick access list to have quicker access to it in the future. Select the desired template, select the Add to quicklist checkbox, and then click OK. For details on managing the Quick access list, and 'Surround With' Templates in general, see Managing and Sharing 'Surround With' Templates.

  4. The template code is inserted. For some templates, you may need to specify template parameters (variables) to complete the use of a template. Please see the example below for a graphic illustration of the process.

Example

Consider the following code fragment:

We would like to enclose the statement in a try..catch block to take care of possible System.IO.IOExceptions thrown. ReSharper conveniently offers a try..catch predefined template to streamline this operation.

Press Ctrl + Alt + J:

We choose the try..catch template in one of the following ways:

The editor now goes into the Template Editing mode. The code looks as follows:

We supply the desired exception type (IOException) at the first input field and press Tab or Enter to move on to the next parameter. At the second input position, we change the name of the exception variable suggested by ReSharper to ioExc  and press Tab or Enter again to leave the Template Editing mode. The caret and selection are moved to the positions specified by the template designer. The code in the editor now looks as follows:

Thus, if the basic exception handling code provided by ReSharper does not suit your needs, simply type your own catch block body in place of the suggested code.

Also, you can modify the try..catch template (or any other template) to your personal taste. See Managing and Sharing 'Surround With' Templates for details.

See Also

Predefined 'Surround With' Templates | Managing and Sharing 'Surround With' Templates