ReSharper 2021.2 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.

Apply 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 at 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 at a 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.

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 Control+Space twice to see 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 (that is, 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.

Configure 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 (Alt+R, O).

  • Show postfix templates is selected on the Code Editing | Postfix Templates page of ReSharper options (Alt+R, O).

To disable specific postfix templates, clear the corresponding check-boxes on the Code Editing | Postfix Templates page of ReSharper options (Alt+R, O).

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

List of postfix templates

ShortcutDescriptionExample
.argSurrounds expression with invocationMethod(expr)
.awaitAwaits expressions of 'Task' typeawait expr
.castSurrounds expression with cast((SomeType) expr)
.elseChecks boolean expression to be 'false'if (!expr)
.fieldIntroduces field for expression_field = expr;
.forIterates over collection with indexfor (var i = 0; i < xs.Length; i++)
.foreachIterates over enumerable collectionforeach (var x in expr)
.forrIterates over collection in reverse with indexfor (var i = xs.Length-1; i >= 0; i--)
.ifChecks boolean expression to be 'true'if (expr)
.lockSurrounds expression with lock blocklock (expr)
.newProduces instantiation expression for typenew SomeType()
.notNegates boolean expression!expr
.notnullChecks expression to be not-nullif (expr != null)
.nullChecks expression to be nullif (expr == null)
.parParenthesizes current expression(expr)
.parseParses string as value of some typeint.Parse(expr)
.propIntroduces property for expressionProperty = expr;
.returnReturns expression from current functionreturn expr;
.selSelects expression in editor|selected + expression|
.switchProduces switch statementswitch (expr)
.throwThrows expression of 'Exception' typethrow expr;
.toAssigns current expression to some variablelvalue = expr;
.tryparseParses string as value of some typeint.TryParse(expr, out value)
.typeofWraps type usage with typeof() expressiontypeof(TExpr)
.usingWraps resource with using statementusing (expr)
.varIntroduces variable for expressionvar x = expr;
.whileIterating while boolean statement is 'true'while (expr)
.yieldYields value from iterator methodyield return expr;
ShortcutDescriptionExample
.elseChecks boolean expression to be 'false'if (!expr)
.forofIterates over an iterable objectfor (let x of expr)
.ifChecks boolean expression to be 'true'if (expr)
.notnullChecks expression to be not-nullif (expr !== null)
.notundefinedChecks expression to be not-undefinedif (expr !== undefined)
.nullChecks expression to be nullif (expr === null)
.returnReturns expression from current functionreturn expr;
.undefinedChecks expression to be undefinedif (expr === undefined)
.varIntroduces variable for expressionvar x = expr;
ShortcutDescriptionExample
.elseChecks boolean expression to be 'false'if (!expr)
.forofIterates over an iterable objectfor (let x of expr)
.ifChecks boolean expression to be 'true'if (expr)
.instanceofChecks instance of expressionif (x instanceof Class)
.notnullChecks expression to be not-nullif (expr !== null)
.notundefinedChecks expression to be not-undefinedif (expr !== undefined)
.nullChecks expression to be nullif (expr === null)
.returnReturns expression from current functionreturn expr;
.typeofChecks type of expressionif (typeof x === 'string')
.undefinedChecks expression to be undefinedif (expr === undefined)
.varIntroduces variable for expressionlet x = expr;
ShortcutDescriptionExample
.beg..endProduces iterators from rangesort(range.begin(), range.end())
.CastSurrounds expression with UE castCast<SomeType>(expr)
.cbeg..cendProduces iterators from rangeis_sorted(range.cbegin(), range.cend())
.co_awaitPasses expression as argument to co_awaitco_await expr
.co_returnReturns expression from current coroutineco_return expr;
.co_yieldPasses expression as argument to co_yieldco_yield expr
.const_castSurrounds expression with const_castconst_cast<SomeType &>(expr)
.doIterating until boolean expression becomes 'false'do { } while (expr);
.dynamic_castSurrounds expression with dynamic_castdynamic_cast<SomeType &>(expr)
.elseChecks boolean expression to be 'false'if (!expr)
.foreachIterates over rangefor (auto && x : range)
.forwardForwards function parameterstd::forward<Arg>(arg)
.ifChecks boolean expression to be 'true'if (expr)
.make_sharedConstructs an object and wraps it in a std::shared_ptrstd::make_shared<SomeType>()
.make_uniqueConstructs an object and wraps it in a std::unique_ptrstd::make_unique<SomeType>()
.newProduces instantiation expression for typenew SomeType()
.reinterpret_castSurrounds expression with reinterpret_castreinterpret_cast<SomeType &>(expr)
.returnReturns expression from current functionreturn expr;
.safe_castSurrounds expression with safe_cast (C++/CLI)safe_cast<SomeType>(expr)
.static_castSurrounds expression with static_caststatic_cast<SomeType>(expr)
.switchProduces switch over integral/enum typeswitch (expr)
.varIntroduces variable for expressionauto x = expr;
.whileIterating while boolean expression is 'true'while (expr)

This feature is supported in the following languages and technologies:

Language: C#Language: VB.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON
Feature is available in C# Feature is not available in Visual Basic .NETFeature is available in C++ Feature is not available in HTML Feature is not available in ASP.NET Feature is not available in RazorFeature is available in JavaScriptFeature is available in TypeScript Feature is not available in CSS Feature is not available in XML Feature is not available in XAML Feature is not available in Resource files Feature is not available in build script files Feature is not available in Protobuf Feature is not available in JSON
Last modified: 19 August 2021