62Microsoft Visual Studio 2010: A Beginner’s Guide

Figure 2-15 The C# for loop snippet template

In the preceding C# loop, i is a variable of type int, the loop will continue to execute as long as i is less than 3, and i will be incremented by one every time after the loop executes. The condition, i < 3, is evaluated before the loop executes, and the loop will not execute if the condition evaluates to false.

The VB For loop initializes i as an integer, iterating (repeating) three times from 0 to 2, inclusive.

The for Loop Snippet

To use the C# for loop snippet, type fo and press TAB, TAB; you’ll see the snippet template in Figure 2-15.

NOTE

The + and & operators from the preceding code example perform string concatenation. Although i is an integer, it will be converted to a string prior to concatenation.

The same key sequence (fo, TAB, TAB) works for VB For loop snippets too, except that you’ll see the snippet template in Figure 2-16.

The C# for loop snippet template is different from previous templates in that you have two fields to fill out. First, name your indexer, which defaults to i, and then press TAB, which moves the focus to the loop size field, containing Length as the placeholder. If you like the variable name i, which is an understood convention, just press the TAB key and set the length of the loop. You’ll end up with a for loop and the carat inside of the block.

For Each Loops

For each loops let you execute a block of code on every value of an array or collection. Arrays store objects in memory as a list. Collections are more sophisticated than arrays

Figure 2-16 The VB For loop snippet template

Page 85
Image 85
Microsoft 9GD00001 manual For Loop Snippet, For Each Loops