CHAPTER 2 BUILDING C# APPLICATIONS
This is a simple example of using the built-in refactorings of Visual Studio, and you’ll see additional
examples here and there over the course of this text. However, feel free to activate the remaining
refactoring options to see their net effect firsthand (remember, we are not going to use the current
VsExample project for anything later in the book, so hack away).
Code Snippets and Surround with Technology
Visual Studio (as well as Visual C# Express) is capable of inserting prefabricated blocks of C# code using
menu selections, context-sensitive mouse clicks, and/or keyboard shortcuts. The number of available
code expansions is impressive and can be broken down into two main groups.
•
Snippets: These templates insert common code blocks at the location of the
mouse cursor.
•
Surround With: These templates wrap a block of selected statements within a
relevant scope.
To see this functionality firsthand, assume that you wish to iterate over the incoming parameters of
the Main() method using a foreach construct. Rather than typing the code in by hand, you can activate
the foreach code snippet. When you have done so, the IDE will dump out a foreach code template at the
current location of the mouse cursor.
To illustrate, place the mouse cursor after the initial opening curly bracket of Main(). One way to
activate a code snippet is to right-click the mouse and activate the Insert Snippet (or Surround With)
menu option. Here, you will find a list of all code snippets of this category (press the Esc key to dismiss
the pop-up menu). As a shortcut, however, you can simply type in the name of the code snippet, foreach
in this case. In Figure 2-15, notice how the icon for a code snippet looks a bit like a torn piece of paper.
Figure 2-15. Activating a code snippet
62