Книга: C# 2008 Programmer

Code Snippets

Code Snippets

The Code Snippet feature in Visual Studio 2008 enables you to insert commonly used code blocks into your project, thereby improving the efficiency of your development process. To insert a code snippet, right-click on the location where you want to insert the code snippet in the Code Editor, and select Insert Snippet (see Figure 2-38).


Figure 2-38

Select the snippet category by clicking on the category name (see the top of Figure 2-39) and then selecting the code snippet you want to insert (see bottom of Figure 2-39).


Figure 2-39 

For example, suppose that you select the try code snippet. The following block of code will be inserted automatically:

private void Form1_Load(object sender, EventArgs e) {
 try {
 } catch (Exception) {
  throw;
 }
}

You can also use the Surround With code snippets feature. Suppose that you have the following statements:

private void Form1_Load(object sender, EventArgs e) {
 int num1 = 5;
 int num2 = 0;
 int result = num1 / num2;
}

The third statement is dangerous because it could result in a division-by-zero runtime error, so it would be good to wrap the code in a try-сatch block. To do so, you can highlight the block of code you want to put within a try-сatch block and right-click it. Select Surround With (see Figure 2-40), and then select the try code snippet.


Figure 2-40 

Your code now looks like this:

private void Form1_Load(object sender, EventArgs e) {
 try {
  int num1 = 5;
  int num2 = 0;
  int result = num1 / num2;
 } catch (Exception) {
  throw;
 }
}

Оглавление книги

Оглавление статьи/книги

Генерация: 1.128. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз