Code templates

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.

ReSharper live templates in C#

Live templates

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.)

Unlike code snippets, ReSharper can predict from the context which variables, fields, and types can be used in corresponding templates. In most cases, you don't need to type anything yourself. Just choose from a list of suggested values. By the way, ReSharper imports Visual Studio code snippets for C# and makes them available as live templates.

ReSharper provides a total of 170+ 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.

ReSharper surround templates in C#

Surround templates

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. 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, which will show a dedicated pop-up with available surround templates. Alternatively, you can press Alt+Enter upon a selection, which will bring up a context menu that contains applicable surround templates as well as additional functionality such as code formatter and context actions.

ReSharper file templates in ASP.NET

File templates

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.

ReSharper postfix templates in C#

Postfix templates

Postfix templates help you transform expressions that you have already typed without jumping backwards. For example, you can wrap the current expression with if, while, lock, using, add return, yield return, 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.

Applying a source template with ReSharper

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 [SourceTemplate] attribute 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.

Template editor

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.

Templates Explorer window

For managing templates, you can use Templates Explorer that you can call by choosing ReSharper | Tools | Templates Explorer.

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.