ReSharper Web Help

ReSharper | Templates Explorer | Surround Templates

This topic lists all predefined Surround templates in ReSharper 8.0.

Surround templates let you quickly enclose an expression, a single statement or an arbitrary block of code with a predefined code construction, such as for loop, or try...catch statement. Surround templates have no shortcuts, they are identified by their descriptions. See Surrounding Code Fragments with Templates for details.

Template Description

C#

namespace Add a namespace to the selected code
if Put the selection into an if statement block
if..else Put the selection into an if statement block and add an else block
while Put the selection into a while loop
for Put the selection into a for loop
do..while Put the selection into a do..while loop
#region Surround the selection with a region
#if..#endif Surround the selection with a pair of preprocessor directives
{} Surround the selection with braces
() Surround the selection with parentheses
try..catch Put the selection into the try block of the try..catch statement
try..finally Put the selection into the try block of the try..finally statement
cast Cast the selection to a specific type
using Put the selection into the using statement
String.Format Integrate the selected format string into the String.Format method call
lock Put the selection into a lock statement

VB.NET

If Put the selection into the statement block following Then in the If-Then construct
If..Else Put the selection into the statement block following Then in the If-Then-Else construct
SyncLock Transform the selection into the SyncLock statement
Using Put the selection into the Using statement
TryCast Wrap the selection into the TryCast
CType Transform the selection into the first parameter of the CType() function
DirectCast Transform the selection into the first parameter of the DirectCast function.
namespace Add a namespace to the selected code

XML

tag Wrap the selection in a tag

HTML

tag Wrap the selection in an HTML tag
link Wrap the selection in a hyperlink

ASP.NET (C#)

foreach foreach block

ASP.NET (VB.NET)

foreach For Each block

JavaScript

{} Surround the selection with braces
() Surround the selection with parentheses

C#

Template Details
namespace

Add a namespace to the selected code

Scope C# 2.0+ types and namespaces

Body

namespace $NAME$ {$END$$SELECTION$}

Parameters

  • NAME - Gets default namespace for the current file
  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__namespace__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__namespace__after

if

Put the selection into an if statement block

Scope C# 2.0+ statements

Body

if($END$){$SELECTION$}

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__if__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__if__after

if..else

Put the selection into an if statement block and add an else block

Scope C# 2.0+ statements

Body

if ($COND$)
{
  $SELECTION$
}
else
{
  $END$
}

Parameters

  • COND - Suggests variables of the specified type.
  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__if..else__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__if..else__after

while

Put the selection into a while loop

Scope C# 2.0+ statements

Body

while($END$){$SELECTION$}

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__while__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__while__after

for

Put the selection into a for loop

Scope C# 2.0+ statements

Body

for($END$){$SELECTION$}

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__for__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__for__after

do..while

Put the selection into a do..while loop

Scope C# 2.0+ statements

Body

do{$SELECTION$}while($END$);

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__do..while__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__do..while__after

#region

Surround the selection with a region

Scope C# 2.0+

Body

#region $END$
$SELECTION$
#endregion

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__region__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__region__after

#if..#endif

Surround the selection with a pair of preprocessor directives

Scope C# 2.0+

Body

#if $END$
$SELECTION$
#endif

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__sharp_if..sharp_endif__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__sharp_if..sharp_endif__after

{}

Surround the selection with braces

Scope C# 2.0+

Body

{
$SELECTION$$END$
}

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__braces__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__braces__after

()

Surround the selection with parentheses

Scope C# 2.0+ expressions

Body

($SELECTION$$END$)

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__parentheses__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__parentheses__after

try..catch

Put the selection into the try block of the try..catch statement

Scope C# 2.0+ statements

Body

try {$SELECTION$} catch ($EXCEPTION$ $EX_NAME$) {
$SELSTART$System.Console.WriteLine($EX_NAME$);$SELEND$
}

Parameters

  • EXCEPTION - Evaluates to the specified constant value.
  • EX_NAME - When exectuted in variable declaration (where variable name should stand), suggests name for the variable.
  • SELECTION - The text selected by the user before invoking the template.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__try..catch__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__try..catch__after

try..finally

Put the selection into the try block of the try..finally statement

Scope C# 2.0+ statements

Body

try {$SELECTION$} finally {
$END$
}

Parameters

  • EXCEPTION - Evaluates to the specified constant value.
  • EX_NAME - When exectuted in variable declaration (where variable name should stand), suggests name for the variable.
  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__try..finally__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__try..finally__after

cast

Cast the selection to a specific type

Scope C# 2.0+ expressions

Body

(($TYPE$)$SELECTION$)

Parameters

  • TYPE - Guess type expected at this point
  • SELECTION - The text selected by the user before invoking the template.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__cast__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__cast__after

using

Put the selection into the using statement

Scope C# 2.0+ statements

Body

using($END$){$SELECTION$}

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__using__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__using__after

String.Format

Integrate the selected format string into the String.Format method call

Scope C# 2.0+ expressions

Body

string.Format($SELECTION$, $END$);

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__String.Format__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__String.Format__after

lock

Put the selection into a lock statement

Scope C# 2.0+ statements

Body

lock ($LOCK$)
{
  $SELECTION$$END$
}

Parameters

  • LOCK - Suggests variables of the specified type.
  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__lock__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__CSharp__lock__after

VB.NET

Template Details
If

Put the selection into the statement block following Then in the If-Then construct

Scope VB.NET 8+ statements

Body

If $END$ Then
  $SELECTION$
End If

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__If__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__If__after

If..Else

Put the selection into the statement block following Then in the If-Then-Else construct

Scope VB.NET 8+ statements

Body

If $COND$ Then
  $SELECTION$
Else
  $END$
End If

Parameters

  • COND - no macro
  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__If..Else__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__If..Else__after

SyncLock

Transform the selection into the SyncLock statement

Scope VB.NET 8+ statements

Body

SyncLock $END$
  $SELECTION$
End SyncLock

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__SyncLock__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__SyncLock__after

Using

Put the selection into the Using statement

Scope VB.NET 8+ statements

Body

Using $END$
  $SELECTION$
End Using

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__using__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__using__after

TryCast

Wrap the selection into the TryCast

Scope VB.NET 8+ expressions

Body

TryCast($SELECTION$, $TYPE$)

Parameters

  • TYPE - Guess type expected at this point
  • SELECTION - The text selected by the user before invoking the template.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__TryCast__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__TryCast__after

CType

Transform the selection into the first parameter of the CType() function

Scope VB.NET 8+ expressions

Body

CType($SELECTION$, $TYPE$)

Parameters

  • TYPE - Guess type expected at this point
  • SELECTION - The text selected by the user before invoking the template.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__CType__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__CType__after

Note

For more details on CType vs. DirectCast, see Visual Basic Language Reference: DirectCast.

DirectCast

Transform the selection into the first parameter of the DirectCast function.

Scope VB.NET 8+ expressions

Body

DirectCast($SELECTION$, $TYPE$)

Parameters

  • TYPE - Guess type expected at this point
  • SELECTION - The text selected by the user before invoking the template.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__DirectCast__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__DirectCast__after

Note

For more details on CType vs. DirectCast, see Visual Basic Language Reference: DirectCast.

namespace

Add a namespace to the selected code

Scope VB.NET 8+ types or namespaces

Body

Namespace $END$
$SELECTION$
End Namespace

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__namespace__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__VB.NET__namespace__after

XML

Template Details
tag

Wrap the selection in a tag

Scope XML tags

Body

<$TAG$$END$>$SELECTION$</$TAG$>

Parameters

  • TAG - Suggests XML tag name used in current document
  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__XML__tag__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__XML__tag__after

HTML

Template Details
tag

Wrap the selection in an HTML tag

Scope HTML and similar tags

Body

<$TAG$$RUNATSERVER$$END$>$SELECTION$</$TAG$>

Parameters

  • TAG - Suggest tag name used in current document
  • RUNATSERVER - Insert runat="server" if server-side tag selected
  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.
link

Wrap the selection in a hyperlink

Scope HTML and similar tags

Body

<a href="http://$END$">$SELECTION$</a>

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

ASP.NET (C#)

Template Details
foreach

foreach block

Scope ASP.NET tags

Body

<% foreach ($TYPE$ $VARIABLE$ in $COLLECTION$) { %>
$SELECTION$
$END$<%}%>

Parameters

  • COLLECTION - Suggests visible variable that can be enumerated (that is, used in foreach loop as collection)
  • TYPE - Suggest type for a new variable declared in the template
  • VARIABLE - When exectuted in variable declaration (where variable name should stand), suggests name for the variable.
  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__ASP.NET__CSharp__foreach__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__ASP.NET__CSharp__foreach__after

ASP.NET (VB.NET)

Template Details
foreach

For Each block

Scope ASP.NET tags

Body

<%For Each $VAR$ As $TYPE$ In $COL$ %>
$SELECTION$
<Next>

Parameters

  • COL - Suggests visible variable that can be enumerated (that is, used in foreach loop as collection)
  • TYPE - Analyzes code and guesses type of element of a collection.
  • VAR - When exectuted in variable declaration (where variable name should stand), suggests name for the variable.
  • SELECTION - The text selected by the user before invoking the template.

Before expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__ASP.NET__VB.NET__foreach__before
After expansion
Reference__Options__Templates__Surround_With_Templates__Predefined__ASP.NET__VB.NET__foreach__after

JavaScript

Template Details
{}

Surround the selection with braces

Scope JavaScript

Body

{
$SELECTION$$END$
}

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.
()

Surround the selection with parentheses

Scope JavaScript

Body

($SELECTION$$END$)

Parameters

  • SELECTION - The text selected by the user before invoking the template.
  • END - The caret position after the template is applied.