ReSharper provides multiple kinds of code templates that help write common code constructs faster, surround existing code with enclosing blocks, or create new files. You can create your own templates specific to your coding habits or your code base.
Live templates are ReSharper's far more intelligent version of Visual Studio's code snippets.
To apply a live template, just type a template abbreviation, and press Tab in the completion list to expand the template. After that, use Enter or Tab to navigate through template parameters. (If you use Visual Studio's native code completion, press Ctrl+E, L to display the list of available live templates and expand a template.)
ReSharper provides a total of 60+ predefined live templates for most supported languages and technologies. The list includes templates for iterating through collections, writing assertion methods, creating new arrays, outputting values, coding LINQ queries, generating auto-properties, and more.
Surround templates are used to quickly enclose an expression, a single statement, or an arbitrary
block of code with
try...catch or other code constructs. ReSharper
intelligently reformats the code, adjusts the selection, and/or repositions the caret
so that you can continue typing immediately after the template is applied.
To surround a piece of code, select it and press Ctrl+E, U.
ReSharper lets you add new files and new sets of files with predefined code fragments. For example, you can create a new file with a class declaration, an interface, struct declaration, and so on. To choose from the list of file templates, press Ctrl+Alt+Ins without even leaving the text editor.
If a file template is designed to create several files, ReSharper will add these files in the specified locations within your solution and initialize their content with the synchronized parameters.
Postfix templates (C# Only)
Postfix templates help you transform expressions that you have already typed without jumping backwards.
For example, you can wrap the current expression with
await in front of the current expression,
iterate over a collection, generate a switch statement, cast the expression to a specific type, or even introduce a field or property for the expression.
For example, as shown in the screenshots, you can type
after any expression and choose the corresponding template in the completion list. ReSharper will then compare
null, wrap the comparison in an 'if' statement, and set your caret in the position where you can continue typing.
Source Templates (C# Only)
You can easily extend the list of postfix templates with custom source templates, which
can be created anywhere in the code of your project as extension methods.
You can define them for some specific types of your project or for any standard types,
as public extension methods decorated with the
from the JetBrains.Annotations framework.
To illustrate usage of source templates, let's create a simple template that will help us check double values for equality to Pi and move your caret to the next line unit tests:
Now we can type the dot after any expression that evaluates to double, and invoke the created template:
When you choose the template in the completion list, the expression is replaced with the template code and the caret shifts as prescribed in the template:
Customizing, Configuring and Sharing Templates
You can create your own templates and modify those that come bundled with ReSharper.
For editing templates, ReSharper provides Template Editor that opens in a separate Visual Studio code pane, allowing you to switch between your code and your template editing environment and make real-world testing of templates faster.
For managing templates, you can use Templates Explorer that you can call by choosing:
Once defined, any template can be easily shared among team members in two ways using the Templates Explorer.
Another way of sharing templates with your team members is using the mechanism of settings layers as code templates are saved in ReSharper settings. To make templates available to other developers who work on the same shared solution, you can either create new code templates in a team-shared settings layer or copy existing templates to a team-shared settings layer.
You can also export code templates to a file and distribute or share it with other developers. Accordingly, you can import code templates created and shared by other developers.
Note on shortcuts
All keyboard shortcuts provided in this page are available in ReSharper's default "Visual Studio" keymap. For details on ReSharper's two keymaps, see ReSharper Documentation.