int result = num1 / num2;
}
IntelliSense
IntelliSense is one of the most useful tools in Visual Studio 2008. IntelliSense automatically detects the properties, methods, events, and so forth of an object as you type in the code editor. You do not need to remember the exact member names of an object because IntelliSense helps you by dynamically providing you with a list of relevant members as you enter your code.
For example, when you type the word Console
in the code editor followed by the .
, IntelliSense displays a list of relevant members pertaining to the Console
class (see Figure 2-41).

Figure 2-41
When you have selected the member you want to use, press the Tab key and IntelliSense will insert the member into your code.
IntelliSense in Visual Studio 2008 has some great enhancements. For example, the IntelliSense dropdown list often obscures the code that is behind when it pops up. You can now make the dropdown list disappear momentarily by pressing the Control key. Figure 2-42 shows the IntelliSense dropdown list blocking the code behind it (top) and having it be translucent by pressing the Control key (bottom).

Figure 2-42
You can also use IntelliSense to tidy up the namespaces at the top of your code. For example, you often import a lot of namespaces at the beginning of your code and some of them might not ever be used by your application. In Visual Studio 2008, you can select the namespaces, right-click, and select Organize Usings (see Figure 2-43).

Figure 2-43
Then you can choose to:
□ Remove all unused using statements
□ Sort the using statements alphabetically
□ Remove all unused using statements and sort the remaining namespace alphabetically
Refactoring Support
Another useful feature available in Visual Studio 2008 is code refactoring. Even though the term may sound unfamiliar, many of you have actually used it. In a nutshell, code refactoring means restructuring your code so that the original intention of the code is preserved. For example, you may rename a variable so that it better reflects its usage. In that case, the entire application that uses the variable needs to be updated with the new name. Another example of code refactoring is extracting a block of code and placing it into a function for more efficient code reuse. In either case, you would need to put in significant amount of effort to ensure that you do not inadvertently inject errors into the modified code. In Visual Studio 2008, you can perform code refactoring easily. The following sections explain how to use this feature.
Renaming variables is a common programming task. However, if you are not careful, you may inadvertently rename the wrong variable (most people use the find-and-replace feature available in the IDE, which is susceptible to wrongly renaming variables). In C# refactoring, you can rename a variable by selecting it, right-clicking, and choosing Refactoring→Rename (see Figure 2-44).

Figure 2-44
You are prompted for a new name (see Figure 2-45). Enter a new name, and click OK.

Figure 2-45
You can preview the change (see Figure 2-46) before it is applied to your code.

Figure 2-46
Click the Apply button to change the variable name.
Very often, you write repetitive code within your application. Consider the following example:
private void Form1_Load(object sender, EventArgs e) {
int num = 10, sum = 0;
for (int i = 1; i <= num; i++) {
sum += i;
}
}
Here, you are summing up all the numbers from 1 to num, a common operation. It would be better for you to package this block of code into a function. So, highlight the code (see Figure 2-47), right-click it, and select Refactor→Extract Method.

Figure 2-47
Supply a new name for your method (see Figure 2-48). You can also preview the default method signature that the refactoring engine has created for you. Click OK.

Figure 2-48
The block of statements is now encapsulated within a function and the original block of code is replaced by a call to that function:
private void Form1_Load(object sender, EventArgs e) {
}
However, you still need to do some tweaking because the variable sum
should be returned from the function. The code you highlight will affect how the refactoring engine works. For example, if you include the variables declaration in the highlighting, a void function is created.
While the method extraction feature is useful, you must pay close attention to the new method signature and the return type. Often, some minor changes are needed to get what you want. Here's another example:
Single radius = 3.5f;