BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Visual Studio
VS 2005+

Visual Studio Looping Code Snippets

Visual Studio 2005 allows quick creation of boilerplate code through the use of code snippets. In this article five snippets are considered. These allow you to quickly create the common looping structures in your programs with simple shortcut phrases.

Code Snippets

Code snippets were introduced in Visual Studio 2005. They allow a short name, or alias, to be typed into a program's source code. The alias is then automatically replaced with some standard code. With some code snippets the inserted code includes placeholders that allow you to quickly change key elements according to your requirements. In this article we will examine five such code snippets, each used to create a looping structure.

NB: The code snippets in this article are those that are created by Visual Studio 2005. The snippets are available in other versions of Visual Studio, including Visual C# Express. However, the expanded text differs slightly in other versions.

The For Loop Snippet

The first code snippet that we will use creates the template code for a simple for loop. To insert the snippet, ensure that your text cursor is within a method, property accessor or other position where a for loop is valid. Type the shortcut, "for" and press the tab key twice. The following code will appear:

for (int i = 0; i < length; i++)
{

}

After the loop is inserted, the loop control variable is selected. If you wish to name this variable anything other than "i", you can simply overtype the name. If you press the tab key for a third time, the "length" text is highlighted. You can then overwrite it with a new length for the loop.

The Reverse For Loop Snippet

Often you will wish to create a loop that starts from a larger number and decrements the control variable's value after each iteration until zero is reached. Such a loop can be inserted using the "forr" code snippet. Again, type the snippet name and press the tab key twice. You can then use the tab key to switch between the control variable name and the length and overtype each as before.

for (int i = length - 1; i >= 0; i--)
{

}

The For-Each Loop Snippet

The next code snippet that we will look at is used to automatically generate a foreach loop. This type of loop includes and control variable and a collection or array of values that will be enumerated. To insert the loop, type "foreach" and press tab twice.

foreach (object var in collection_to_loop)
{

}

The standard foreach loop code assumes that you will be looping through a collection of objects. Generally you will wish to be more specific about the type of object that you will be working with. For this reason, the type is highlighted first and may be replaced with the actual type required. Pressing tab again highlights the control variable name and a fourth press selects the collection name for overtyping.

The While Loop Snippets

The last two looping code snippets create the boilerplate code for the while loops. In each case, only the condition will be highlighted automatically for overtyping. The first snippet creates a while loop that permits the condition to be checked before the contents are processed, with the potential for the contained code to never be executed.

To insert the code shown below, type "while" and press tab twice.

while (true)
{

}

The second variation of the while loop is inserted when you type "do" and press the tab key twice. This version checks the condition only after the first execution of the contained code.

do
{

} while (true);
11 June 2009