ReSharper 2017.3 Help

Postfix Templates

Postfix templates help you transform expressions that you have already typed without jumping backwards - just type a dot after an expression and pick a template from the completion list.

Applying postfix templates

One of the simplest examples of postfix templates is negating a boolean expression. Suppose you have just typed a boolean expression and then realized that the comparison logic should be reversed. Normally, you have to move your caret back and change == to != or vice versa, and then return to where you were. With ReSharper, you can just continue typing .not and press Enter.

There are a lot more postfix templates that you can use to speed up coding. 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.

Postfix template could even change your typing routine. Consider the CheckInput method below and imagine how you would type the null-checking clause.

private void CheckInput(string input) { if (input == null) { throw new ArgumentNullException("The input is null"); } // do something with 'input' }

Now let's see how you could do it with postfix templates.

As soon as your caret is in the method body, you can start typing input right away because that is what you want to check. When the input is there, just continue typing .null - a postfix template for checking the expression for null:

Applying postfix template for null-check

This will compare input with null and wrap the comparison in an if statement, and set your caret in the position where you can continue typing:

Applying postfix template for null-check

Now, instead of typing throw, you can go ahead with typing the exception class name, and invoke the .throw template after it:

Applying postfix template for null-check

After applying this template, you have the complete throw statement, and the caret in place to enter arguments:

Applying postfix template for null-check

You could see that instead of typing frequently used language constructs manually, in many cases you can type just a couple of initial characters of a template shortcut and get everything in place, properly formatted and without typos.

To apply a postfix template

  1. Type a dot after the current expression and check the completion list for the desired templates.
  2. If you know the shortcut of the template that you are going to apply, start typing it - this will shrink the list of suggestions.
  3. Depending on the current context, some postfix templates may not be included into the completion list. If you do not see the desired template, but still want to use it, press Ctrl+Space twice (Double Completion) to add all templates to the completion list.
  4. As soon as the desired template is selected in the suggestion list, press Enter.
  5. If the template has editable parameters (i.e. requires user input), ReSharper deploys a Hot Spot Session in the editor and sets the input position at the first parameter. Then you can do the following:
    • If ReSharper suggests some values for the current parameter, use Up and Down arrow keys to navigate through the list of suggested values, or just type in a desired value.
    • Press Tab or Enter to accept the value and move to the input position of the next parameter. If this is the last parameter, the hot spot session completes and the caret moves to the end position defined for the session.
    • Press Shift+Tab to move the input focus to the input position of the previous parameter.
    • Press Esc to exit the hot spot session. In this case, all session parameters will be initialized with default values.

Configuring postfix templates

Postfix templates are enabled by default. They appear in the completion list if:

  • ReSharper IntelliSense is enabled on the Environment | IntelliSense | General page of ReSharper options.
  • Show postfix templates is selected on the Code Editing | Postfix Templates page of ReSharper options.

To disable specific postfix templates, clear the corresponding check-boxes on the Code Editing | Postfix Templates page of ReSharper options.

To hide templates in the completion list or, conversely, hide all other items, use completion filters.

List of postfix templates

ShortcutDescriptionExample
.arg Surrounds expression with invocation Method(expr)
.await Awaits expressions of 'Task' type await expr
.cast Surrounds expression with cast ((SomeType) expr)
.else Checks boolean expression to be 'false' if (!expr)
.field Introduces field for expression _field = expr;
.for Iterates over collection with index for (var i = 0; i < xs.Length; i++)
.foreach Iterates over enumerable collection foreach (var x in expr)
.forr Iterates over collection in reverse with index for (var i = xs.Length-1; i >= 0; i--)
.if Checks boolean expression to be 'true' if (expr)
.lock Surrounds expression with lock block lock (expr)
.new Produces instantiation expression for type new SomeType()
.not Negates boolean expression !expr
.notnull Checks expression to be not-null if (expr != null)
.null Checks expression to be null if (expr == null)
.par Parenthesizes current expression (expr)
.parse Parses string as value of some type int.Parse(expr)
.prop Introduces property for expression Property = expr;
.return Returns expression from current function return expr;
.sel Selects expression in editor |selected + expression|
.switch Produces switch statement switch (expr)
.throw Throws expression of 'Exception' type throw expr;
.to Assigns current expression to some variable lvalue = expr;
.tryparse Parses string as value of some type int.TryParse(expr, out value)
.typeof Wraps type usage with typeof() expression typeof(TExpr)
.using Wraps resource with using statement using (expr)
.var Introduces variable for expression var x = expr;
.while Iterating while boolean statement is 'true' while (expr)
.yield Yields value from iterator method yield return expr;
ShortcutDescriptionExample
.else Checks boolean expression to be 'false' if (!expr)
.forof Iterates over an iterable object for (let x of expr)
.if Checks boolean expression to be 'true' if (expr)
.notnull Checks expression to be not-null if (expr !== null)
.notundefined Checks expression to be not-undefined if (expr !== undefined)
.null Checks expression to be null if (expr === null)
.return Returns expression from current function return expr;
.undefined Checks expression to be undefined if (expr === undefined)
.var Introduces variable for expression var x = expr;
ShortcutDescriptionExample
.else Checks boolean expression to be 'false' if (!expr)
.forof Iterates over an iterable object for (let x of expr)
.if Checks boolean expression to be 'true' if (expr)
.instanceof Checks instance of expression if (x instanceof Class)
.notnull Checks expression to be not-null if (expr !== null)
.notundefined Checks expression to be not-undefined if (expr !== undefined)
.null Checks expression to be null if (expr === null)
.return Returns expression from current function return expr;
.typeof Checks type of expression if (typeof x === 'string')
.undefined Checks expression to be undefined if (expr === undefined)
.var Introduces variable for expression let x = expr;
ShortcutDescriptionExample
.beg..end Produces iterators from range sort(range.begin(), range.end())
.cbeg..cend Produces iterators from range is_sorted(range.cbegin(), range.cend())
.const_cast Surrounds expression with const_cast const_cast<SomeType &>(expr)
.do Iterating until boolean expression becomes 'false' do { } while (expr);
.dynamic_cast Surrounds expression with dynamic_cast dynamic_cast<SomeType &>(expr)
.else Checks boolean expression to be 'false' if (!expr)
.foreach Iterates over range foreach (auto && x in range)
.if Checks boolean expression to be 'true' if (expr)
.make_shared Constructs an object and wraps it in a std::shared_ptr std::make_shared<SomeType>()
.make_unique Constructs an object and wraps it in a std::unique_ptr std::make_unique<SomeType>()
.new Produces instantiation expression for type new SomeType()
.reinterpret_cast Surrounds expression with reinterpret_cast reinterpret_cast<SomeType &>(expr)
.return Returns expression from current function return expr;
.static_cast Surrounds expression with static_cast static_cast<SomeType>(expr)
.switch Produces switch over integral/enum type switch (expr)
.var Introduces variable for expression auto x = expr;
.while Iterating while boolean expression is 'true' while (expr)

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 not available Feature is available Feature is not available Feature is not available Feature is not available Feature is available Feature is available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available
Last modified: 16 April 2018