ReSharper Web Help

ReSharper | Live Templates | Live Templates | Predefined Templates | C# | Iteration

C#

Iteration

Templates in this group are used for iterating over various containers. Since these templates are parameterized, you will need to fill in some parameters (variables) after the template is expanded. These input fields normally include:

  • Loop index variable
  • Number of iterations
  • Container instance

To help you complete the necessary input fields, ReSharper suggests a list of choices. They include names of variables, instances of arrays, lists available in scope etc.

Template name Description
for

Go to top

Purpose
Creates a general-purpose for loop.

for (int $INDEX$ = 0; $INDEX$ < $UPPER$; $INDEX$++)
{
    $END$
}

Before expansion
loop (before)
After expansion
loop (after)

Parameters

  • i - loop index variable.
  • UPPER - number of iterations.
After you specify the parameters, the caret is positioned so that you can continue coding the loop body.

foreach

Go to top

Purpose
Generates a foreach block which iterates over an instance of System.Collections.IEnumerable.

foreach ($TYPE$ $VARIABLE$ in $COLLECTION$)
{
    $END$
}

Before expansion
Reference__Options__Templates__Live_Templates__Predefined__CSharp__Iteration__foreach__before.png
After expansion
Reference__Options__Templates__Live_Templates__Predefined__CSharp__Iteration__foreach__after.png

Parameters

  • COLLECTION - instance of IEnumerable to iterate over (in the example above, ReSharper suggests the list of collections available in the current scope).
  • TYPE - type to cast the array elements to.
  • VARIABLE - iterator variable (in the example above, ReSharper suggests array based on the type name).
After parameters are specified, the caret is positioned so that you can continue writing the loop body.

ital

Go to top

Purpose
Generates a forloop which iterates over an instance ofSystem.Collections.ArrayList. Includes a customizable statement in the loop body with a variable to hold a current array element.

for (int $INDEX$ = 0; $INDEX$ < $ARRAY$.Count; $INDEX$++)
{
    $TYPE$ $VAR$ = ($TYPE$)$ARRAY$[$INDEX$];
    $END$
}

Before expansion
ital (before)
After expansion
ital (after)

Parameters

  • INDEX - name of index variable.
  • ARRAY - an ArrayList to iterate over (in the example above, myList is automatically suggested by ReSharper).
  • TYPE - type of variable in the loop body which is used to hold the current ArrayList element.
  • VAR - name of variable in the loop body which is used to hold the current ArrayList element (ReSharper makes its suggestions based on the name of the array).
After you specify the parameters, the caret is positioned so that you can continue coding the loop body.

itar

Go to top

Purpose
Generates a for loop for iterating over an array. Includes a customizable statement in the loop body with a variable to hold a current array element.

for (int $INDEX$ = 0; $INDEX$ < $ARRAY$.Length; $INDEX$++)
{
    $TYPE$ $VAR$ = $ARRAY$[$INDEX$];
    $END$
}

Before expansion
itar (before)
After expansion
itar (after)

Parameters

  • INDEX - name of index variable.
  • ARRAY - array to iterate over (in the example above, ReSharper suggests arrayOfStrings, an array available in the current scope).
  • TYPE - type of variable in the loop body that the current array element is assigned to (ReSharper usually suggests a value for this parameter based on the array type).
  • VAR - name of variable to hold the current array element (ReSharper suggests a value for this parameter based on the type name, for example, s for string).
After you specify the parameters, the caret is positioned so that you can continue coding the loop body.

itdg

Go to top

Purpose
Generates a foreach block which iterates over a System.Collections.Generic.IDictionary collection. Includes two customizable statements in the loop body with two variables holding the key and the value of a dictionary element.


foreach ($DETYPE$ $ENTRY$ in $DICT$)
{
    $KEYTYPE$ $KEY$ = $ENTRY$.Key;
    $VALTYPE$ $VAL$ = $ENTRY$.Value;
    $END$
}

Before expansion
itdf (before)
After expansion
itdg (after)

Parameters

  • DETYPE - type of IDictionary entries to iterate over.
  • ENTRY - iterator variable.
  • DICT - name of dictionary instance to iterate over (in the example above, ReSharper suggests myDict because it is available in the current scope).
  • KEYTYPE - type of variable used to store the key of a dictionary element.
  • KEY - name of variable used to store the key of a dictionary element.
  • VALTYPE - type of variable used to store the value of a dictionary element.
  • VAL - name of variable used to store the value of a dictionary element.
After you specify the parameters, the caret is positioned so that you can continue coding the loop body.

itdic

Go to top

Purpose
Generates a foreach block which iterates over a System.Collections.IDictionary collection. Unlike itdg, it provides fully-qualified type name for iterator variable in the loop condition and performs type casts over the key and the value of a dictionary element in the loop body. Includes two customizable statements in the loop body with two variables holding the key and the value of a dictionary element.

foreach (System.Collections.DictionaryEntry $ENTRY$ in $DICT$)
{
    $KEYTYPE$ $KEY$ = $CASTKEY$$ENTRY$.Key;
    $VALTYPE$ $VAL$ = $CASTVALUE$$ENTRY$.Value;
    $END$
}

Before expansion
itdic (before)
After expansion
itdic (after)

Parameters

  • DIC - name of dictionary instance to iterate over.
  • ENTRY - iterator variable.
  • KEYTYPE - type of variable used to store the key of a dictionary element.
  • KEY - name of variable used to store the key of a dictionary element.
  • CASTKEY - type to which the dictionary element key should be cast.
  • VALTYPE - type of variable used to store the value of a dictionary element.
  • VAL - name of variable used to store the value of a dictionary element.
  • CASTVALUE - type to which the dictionary element value should be cast.
After you specify the parameters, the caret is positioned so that you can continue coding the loop body.

itli

Go to top

Purpose
Generates a for loop which iterates over an instance of System.Collections.IList. Includes a customizable statement in the loop body with a variable holding container elements.

for (int i = 0; i < $LIST$.Count; i++)
{
    $TYPE$ $ITEM$ = $LIST$[i];
    $END$
}

Before expansion
itli (before)
After expansion
itli (after)

Parameters

  • LIST - instance of a IList-implementing collection to iterate over.
  • TYPE - type of variable used to store the value of a IList element.
  • ITEM - name of variable used to store the value of a IList element.
After you specify the parameters, the caret is positioned so that you can continue coding the loop body.

ritar

Go to top

Purpose
Generates a for loop for iterating over an array in reverse order. Includes a customizable statement in the loop body with a variable holding container elements.

for (int $INDEX$ = $ARRAY$.Length - 1; $INDEX$ >= 0; $INDEX$--)
{
    $TYPE$ $VAR$ = $ARRAY$[$INDEX$];
    $END$
}

Before expansion
ritar (before)
After expansion
ritar (after)

Parameters

  • INDEX - iterator variable.
  • ARRAY - array instance to iterate over (in the example above, arrayOfStrings has been automatically suggested by ReSharper because it's available in the scope).
  • TYPE - type to cast the array elements (in the example above, string type is usually suggested by ReSharper based on the type of the array).
  • VAR - variable to hold the current array element (ReSharper makes some suggestions based on the type name, for example, s for string).
After you specify the parameters, the caret is positioned so that you can continue coding the loop body.

See Also

Procedures

Reference