CODE SNIPPETS

A code snippet is a piece of code that you might find useful in many applications. It is stored in a snippet library so that you can quickly insert it into a new application.

Visual Studio comes with hundreds of snippets for performing standard tasks. Before you start working on a complicated piece of code, you should glance at the snippets that are already available to you. In fact, it would be worth your time to use the Code Snippet Manager available from the Tools menu to take a good look at the available snippets right now before you start a new project. There’s little point in reinventing methods for calculating statistical values if someone has already done it and given you the code.

To insert a snippet, right-click where you want to insert the code and select Insert Snippet to make the editor display a list of snippet categories. Double-click a category to find the kinds of snippets that you want. If you select a snippet, a tooltip pops up to describe it. Figure 5-8 shows the editor preparing to insert the snippet named “Inserts a test method” from the Test snippet category.

FIGURE 5-8: When you select a code snippet, a pop-up describes it.

image

Double-click the snippet to insert it into your code.

The snippet may include values that you should replace in your code. These replacement values are highlighted with a light green background, and the first value is initially selected. If you hover the mouse over one of these values, a tooltip appears to describe the value. You can use the Tab key to jump between replacement values.

Figure 5-9 shows the code inserted by a snippet that creates a new property. The text newPropertyValue is highlighted and selected. Other selected text includes String and NewProperty. The mouse is hovering over newPropertyValue, so the tooltip explains that value’s purpose.

FIGURE 5-9: Values that you should replace in a snippet are highlighted.

image
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.135.231