Code Editor

Code Editor

In Visual Studio 2005, the Code Editor has been enhanced. Developers can add, format, and edit code with the Code Editor. Code is often organized by file, region, and color. Some of the improvements to the Code Editor include better IntelliSense, code snippets, smart tags, and additional formatting options.


IntelliSense helps developers enter code correctly and efficiently. It minimizes the keystrokes required to enter code while improving developer accuracy. IntelliSense is available in both the Code Editor and the immediate mode command window. IntelliSense is actually a group of related features that includes the completion list, parameter information, quick info, and complete word.

IntelliSense is a dynamic drop-down list called the completion list and appears as developers type new commands or words. The completion list automatically appears after a space separator or dot for a member is typed. You can also force a completion list at a dot or the Ctrl+spacebar shortcut key. The completion list contains items for namespaces, types, type members, language keywords, and code snippets. As text is entered, the first matching item in the list is selected. As more text is entered, the match is refined. When the desired item is selected, press Tab to insert. Assuming that the IntelliSense For Most Recently Used Members option is enabled, recently used items that match the input text are selected first. Function overloads are not shown in the completion list. For example, there are 19 overloads of the Console.WriteLine method. However, WriteLine appears in the member list just once. The overload versions of a function, if any, are displayed with the IntelliSense for parameter info.

Parameter Info, a form of IntelliSense, displays the parameters of a function, including the overloaded versions of the function, which have different signatures. Use the arrows to cycle through the available overloads (shown in Figure 4-23). Parameter Info is prompted at the open parentheses of a function. Within function parentheses, the Ctrl+Shift+spacebar key-stroke prompts Parameter Info.

Image from book
Figure 4-23: Parameter info for Console.WriteLine

IntelliSense in Visual Studio 2005 auto detects generics types and arguments. (See Figure 4-24.) A generic dictionary is defined that has string keys and integer values. When creating a new instance of the generic type, IntelliSense filters the completion list. The completion list automatically highlights the correct generic, key, and value types.

Image from book
Figure 4-24: IntelliSense for a generic type

There are other circumstances in which IntelliSense filters the completion list:

  • Interfaces

  • Base classes

  • Attributes

  • As and is operators

  • Catch clauses

Add using Have you ever attempted to use a type without the proper using statement? It requires stopping, determining the correct namespace, and then explicitly entering the namespace before the type or adding a using statement. The Add Using feature of IntelliSense avoids the distraction of resolving unbound types. This is yet another way IntelliSense improves developer productivity.

When an unbound type is entered, a smart tag, which appears as a narrow red box, is placed beneath the last character. The smart tag is shown when the cursor is in or immediately adjacent to the unbound type. From the smart tag menu, you have two choices: Either insert the using statement or prefix the unbound type with the required namespace. Figure 4-25 shows the smart tag menu. Alternatively, open a shortcut menu on the unbound type, choose the Resolve submenu, and then choose from the two menu choices.

Image from book
Figure 4-25: Add Using smart tag menu

Surround With

Visual Studio allows developers to surround code with an item on the completion list. You can surround a block of code with a region. A block of statements can be surrounded with a for loop. The Surround With feature surrounds selected text with something from the completion list. First select the text to be surrounded. Open a shortcut menu on the selected text and choose the Surround With command. The Surround With command will display the completion list. Select the item that should surround the text.

Font and Color Formatting

Visual Studio has always allowed developers to customize the color scheme of the user interface. Visual Studio 2005 .NET extends custom formatting to user types, user keywords, and other information. This is done in the Options dialog box, which is opened from the Tools menu. In the dialog box, switch to the Environment window and select Fonts and Colors. Figure 4-26 shows the Fonts and Colors window.

Image from book
Figure 4-26: Fonts and Colors window

Source Code Formatting

Visual Studio 2005 provides developers additional control over code formatting. Developers control code indentation, new line spacing, code spacing, and the wrapping of blocks. This is done in the Options dialog box on the Tools menu. In the dialog box, open the Formatting window in the Text Editor section under the C# node. (See Figure 4-27.) Except for the General node, the bottom-right pane is the code preview area, in which options can be reviewed before accepting.

Image from book
Figure 4-27: Source Formatting window

Change Tracking

Change tracking distinguishes saved from unsaved code. Visual Studio 2005 colorizes the far left pane to indicate the status of code. Saved code is highlighted in green. Unsaved code is marked in yellow. Original code, which is code unchanged since the source file was opened, is not highlighted. This is demonstrated in Figure 4-28.

Image from book
Figure 4-28: Change tracking of code


Almost every aspect of the Visual Studio 2005 user interface is customizable. Some developers spend considerable time creating the ideal look and feel. They might want to preserve these settings to reapply when necessary. User interface settings are saved in a profile, which includes window and text settings. The profile is particularly useful when reinstalling Visual Studio because the user interface settings are lost at that time. The profile is a convenient tool for reasserting preferred user preferences. The profile is helpful when you want to configure the Visual Studio user interface similarly on different machines. Developers can share profiles with other developers to identically configure their machines. This is helpful when developers are working together on a project.

Profiles can be exported or imported in the Import And Export Settings Wizard dialog box on the Tools menu. In the dialog box, you can import, export, or reapply the defaults of the user interface. (See Figure 4-29.) Exporting the user interface settings creates a profile, whereas importing reads a profile that is applied to the user interface. The Import And Export Settings Wizard dialog box guides the user through the process.

Image from book
Figure 4-29: Import And Export Settings Wizard dialog box