IntelliSense is the name applied to a collection of different coding aids surfaced within the text editor window. Its sole purpose is to help you, the developer, write a syntactically correct line of code quickly. In addition, it tries to provide enough guidance to help you write lines of code that are correct in context—that is, code that makes sense given the surrounding lines of code.

As you type within the text editor, IntelliSense is the behind-the-scenes agent responsible for providing a list of code fragments that match the characters you have already entered, highlighting/preselecting the one that makes the most sense given the surrounding context, and, if so commanded, automatically inserting that code fragment in-line. This saves you the time of looking up types and members in the reference documentation and saves time again by inserting code without your having to actually type the characters for that code.

There are many discrete pieces to IntelliSense that seamlessly work in conjunction with one another as you are writing code. You can also trigger all of these IntelliSense features directly from the Edit, IntelliSense menu, or by pressing Ctrl+space. And many of them can be found as well on the text editor’s context menu, or by right-clicking anywhere in the editor window. Let’s look at them one by one.

Complete Word
Complete Word is the basic timesaving kernel of IntelliSense. After you have typed enough characters for IntelliSense to recognize what you are trying to write, a guess is made as to the complete word you are in the process of typing. This guess is then presented to you within a list of possible alternatives (referred to as the completion list) and can be inserted into the code editor with one keystroke. This is in contrast to your completing the word manually by typing all of its characters.

Based on the context of the code and based on the characters typed into the editor, a list of possible words is displayed. One of these words is selected as the most viable candidate; you may select any entry in the list (via the arrow keys or the mouse). Pressing the Tab key automatically injects the word into the editor for you.

You can manually invoke Complete Word at any time by using the Ctrl+space or
Alt+right-arrow key combinations.

Complete Word takes the actual code context into account for various situations. For instance, if you are in the midst of keying in the exception type in a try/catch block, IntelliSense displays only exception types in the completion list. Likewise, typing an attribute triggers a completion list filtered only for attributes; when you’re implementing an interface, only interface types are displayed; and so on. This IntelliSense feature is enabled for all sorts of content: Beyond C# and Visual Basic code, IntelliSense completion works for other files as well, such as HTML tags, CSS style attributes, .config files, and HTML script blocks, just to name a few. Visual Basic offers functionality with
Complete Word that C# does not: It provides a tabbed completion list, in which one tab contains the most commonly used syntax snippets, and the other contains all the possible words.

Quick Info
Quick Info displays the complete code declaration and help information for any code construct. You invoke it by hovering the mouse pointer over an identifier; a pop-up box displays the information available for that identifier. You are provided with the declaration syntax for the member, a brief description of the member, and a list of its exception classes. The description that shows up in the Quick Info balloon also works for code that you write: If you have a code comment associated with a member, IntelliSense parses the comment and uses it to display the description information.

List Members
The List Members feature functions in an identical fashion to Complete Word; for any given type or namespace, it displays a scrollable list of all valid member variables and functions specific to that type. To see the List Members function in action, perform the following steps in an open code editor window:

1. Type the name of a class (Ctrl+spacebar will give you the IntelliSense window with
possible class names).

2. Type a period; this indicates to IntelliSense that you have finished with the type name and are now “scoped in” to that type’s members.

3. The list of valid members is now displayed. You can manually scroll through the list and select the desired member at this point, or, if you are well aware of the member you are trying to code, you can simply continue typing until IntelliSense has captured enough characters to select the member you are looking for.

4. Leverage Complete Word by pressing the Tab key to automatically insert the member into your line of code (thus saving you the typing effort).

This feature also operates in conjunction with Quick Info: As you select different members in the members list, a Quick Info pop-up is displayed for that member.

Parameter Info
Parameter Info, as its name implies, is designed to provide interactive guidance for the parameters needed for any given function call. This feature is especially useful for making function calls that have a long list of parameters and/or a long overload list. Parameter Info is initiated whenever you type an opening parenthesis after a function name. To see how this works, perform these steps:

1. Type the name of a function.

2. Type an open parenthesis.

3. A pop-up box shows the function signature. If there are multiple valid signatures (for example, multiple overloaded versions of this function), you can scroll through the different signatures by using the small up- and down-arrow cues. Select the desired signature.

4. Start typing the actual parameters you want to pass in to the function. As you type, the parameter info pop-up continues coaching you through the parameter list by bolding the current parameter you are working on. As each successive parameter is highlighted, the definition for that parameter appears.

Organize Usings
Organize Usings is a C#-only IntelliSense item. It encapsulates two separate functions: Remove Unused Usings and Sort Usings. It also provides a third command, Remove and Sort, that combines the two actions into one. All three commands live under the Organize Usings menu item on the editor shortcut menu, or under the main Edit, IntelliSense menu.

The Remove Unused Usings function is a great aid for uncluttering your code. It parses through the current body of code and determines which Using statements are necessary for the code to compile; it then removes all other Using statements. The Sort command is straightforward as well: It simply rearranges all of your Using statements so that they appear in A–Z alphabetical order by namespace.

Code Snippets and Template Code
Code snippets are prestocked lines of code available for selection and insertion into the text editor. Each code snippet is referenced by a name referred to as its alias. Code snippets are used to automate what would normally be non–value-added, repetitive typing.
You can create your own code snippets or use the default library of common code elements provided by Visual Studio.

Using the Code Snippet Inserter
You insert snippets by right-clicking at the intended insertion point within an open text editor window and then selecting Insert Snippet from the shortcut menu. This launches the Code Snippet Inserter, which is a drop-down (or series of drop-downs) that works much like the IntelliSense Complete Word feature. Each item in the inserter represents a snippet, represented by its alias. Selecting an alias expands the snippet into the active document.

Each snippet is categorized to make it easier to find the specific piece of code you are looking for. As an example, to insert a constructor snippet into a C# class, we would rightclick within the class definition, select Visual C# from the list of snippet categories, and hen select ctor. Noe that as you select a snippet category, a placeholder is displayed in the text editor window to help establish a “bread-crumb” trail.

After the constructor snippet is expanded into the text editor, you will still, of course, have to write meaningful code inside of the constructor; but, in general, snippets eliminate the process of tedious coding that really doesn’t require much intellectual horsepower to generate. The process is identical with the exception that Visual Basic makes more extensive use of categories.

Surrounding Code with Snippets
C# and XML documents have one additional style of code snippets that bears mentioning:
Surround With snippets. Surround With snippets are still snippets at their core (again, these are simply prestocked lines of code), but they differ in how they are able to insert themselves into your code.

Using a Surround With snippet, you can stack enclosing text around a selection with the text editor. As an example, perhaps you have a few different class declarations that you would like to nest within a namespace. When you use the Surround With snippet, this is a simple two-step process: Highlight the class definitions and fire up the Code Snippet Inserter. This time, instead of selecting Insert Snippet from the shortcut menu, you select
Surround With. The insert works the same way, but this time has applied the snippet (in this case, a namespace snippet) in a different fashion. Compare the before and after text. We have encapsulated the class definitions within a new namespace that sits within yet another namespace—all with just a few mouse clicks.

Creating Your Own Code Snippets
Because code snippets are stored in XML files, you can create your own snippets quite easily. The key is to understand the XML schema that defines a snippet, and the best way to do that is to look at the XML source data for some of the snippets included with the IDE. Snippets are stored on a per-language basis under the install directory for Visual Studio. For example, the Visual Basic snippets can be found, by default, in the folders under the C:\Program Files\Microsoft Visual Studio 9.0\Vb\Snippets directory. Although snippet files are XML, they carry a .Snippet extension.

The XML Snippet Format
The basic structure of this particular snippet declaration is described in below. A more complete schema reference is available as a part of the Visual Studio MSDN help collection; it is located under Integrated Development Environment for Visual Studio, Reference, XML Schema References, Code Snippets Schema Reference.

<CodeSnippets>. The parent element for all code snippet information. It references the specific XML namespace used to define snippets within Visual Studio

<CodeSnippet>. The root element for a single code snippet. This tag sets the format version information for the snippet (for the initial release of VS 2008, this should be set to 1.0.0). Although multiple CodeSnippet elements are possible within the parent <CodeSnippets> element, the convention is to place one snippet per file.

<Header>. A metadata container element for data that describes the snippet.

<Title>. The title of the code snippet.

<Shortcut>. Typically, the same as the title, this is the text that will appear in the code snippet insertion drop-downs.

<Description>. A description of the snippet.

<Author>. The author of the snippet.

<SnippetTypes>. The parent element for holding elements describing the snippet’s type.

<SnippetType>. The type of the snippet: Expansion, Refactoring, or Surrounds With. You cannot create custom refactoring snippets. This property is really used to tell Visual Studio where the snippet can be inserted within the editor window: Expansion snippets insert at the current cursor position, whereas Surrounds With snippets get inserted before and after the code body identified by the current cursor position or selection.

<Snippet>. The root element for the snippet code.

<Declarations>. The root element for the literals and objects used by the snippet.

<Literal>. A string whose value can be interactively set as part of the snippet expansion process. The Editable attribute on this tag indicates whether the literal is static or editable. The ctor snippet is an example of one without an editable literal; contrast this with the form transparency snippet that you saw—an example of a snippet with an editable literal that allows you to set the form name as part of the snippet insertion.

<ID>. A unique ID for the literal.

<ToolTip>. A ToolTip to display when the cursor is placed over the literal.

<Function>. The name of a function to call when the literal receives focus. Functions are available only in C# snippets.

<Default>. The default string literal to insert into the editor.

<Code>. An element that contains the actual code to insert.

Code Snippet Functions - calling a function to prepopulate the class name within the nippet. Functions are available only with C# (with a subset also available in J#). The rest of the literals rely on the developer to type over the placeholder value with the correct value.

GenerateSwitchCases. Creates the syntax for a switch statement that includes a case statement for each value defined by the enumeration represented by enumliteral (C#/J#).

ClassName(). Inserts the name of the class containing the code snippet (C#/J#).

SimpleTypeName. Takes the type name referenced by typename and returns the shortest name possible given the using statements in effect for the current code block.

CallBase. Is useful when stubbing out members that implement or return the base type: When you specify get, set, or method as the parameter, a call will be created against the base class for that specific property accessor or method (C#).

Adding a Snippet to Visual Studio You can use Visual Studio’s own XML editor to create the XML document and save it to a directory (a big bonus for doing so is that you can leverage IntelliSense triggered by the XML snippet schema to help you with your element names and relationships). The Visual Studio installer creates a default directory to place your custom snippets located in your Documents folder: user\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets. If you place your XML template here,

Visual Studio will automatically include your snippet for use. The Code Snippets Manager, which is launched from the Tools menu, is the central control dialog box for browsing the available snippets, adding new ones, or removing a snippet. You can also opt to include other folders besides the standard ones. To do so, click on the Add button to enter additional folders for Visual Studio to use when displaying the list of snippets.

Snippets in the Toolbox
Although this capability is technically not part of the official code snippet technology within Visual Studio, you can also store snippets of code in the toolbox. First, select the text in the editor and then drag and drop it onto the toolbox. You can then reuse this snippet at any time by dragging it back from the toolbox into an open editor window.

Brace Matching
Programming languages make use of parentheses, braces, brackets, and other delimiters to delimit function arguments, mathematical functions/order of operation, and bodies of code. It can be difficult to visually determine whether you have missed a matching delimiter—that is, if you have more opening delimiters than you have closing delimiters—especially with highly nested lines of code.

Brace matching refers to visual cues that the code editor uses to make you aware of your matching delimiters. As you type code into the editor, any time you enter a closing delimiter, the matching opening delimiter and the closing delimiter will briefly be highlighted.

Although this feature is referred to as brace matching, it actually functions with the following delimiters:
• Parentheses: ()
• Brackets: [], <>
• Quotation marks: ””
• Braces: {}

In the case of C#, brace matching also works with the following keyword pairs (which essentially function as delimiters using keywords):
• # region, #endregion
• #if, #else, #endif
• case, break
• default, break
• for, break, continue
• if, else
• while, break, continue

Customizing IntelliSense
Certain IntelliSense features can be customized, on a per-language basis, within the Visual Studio Options dialog box. If you launch the Options dialog box (located under the Tools menu) and then navigate to the Text Editor node, you will find IntelliSense options confusingly scattered under both the General and IntelliSense pages.

Completion Lists in this dialog box refer to any of the IntelliSense features that facilitate autocompletion of code, such as List Members and Complete Word are such as:

Show Completion List After a Character Is Typed. This causes the Complete Word feature to automatically run after a single character is typed in the editor window.

Place Keywords in Completion Lists. If this box is checked, language keywords will be displayed within the completion list. As an example, for C#, this would cause keywords such as class or string to be included in the completion list.

Place Code Snippets in Completion Lists. Checking this box will place code-snippet alias names into any displayed completion lists.

Committed by Typing the Following Characters. This check box contains any of the characters that will cause IntelliSense to execute a completion action. In other words, typing any of the characters in this text box while a completion list is displayed will cause IntelliSense to insert the current selection into the editor window.

Committed by Pressing the Space Bar. Checking this adds the space character to the list of characters that will fire a completion commit.

Add New Line on Commit with Enter at End of Fully Typed Word. Checking this box will cause the cursor to advance down an entire line if you fully type a word in the IntelliSense list box. This is useful for those scenarios in which fully typed keywords are unlikely to be followed on the same line with other code.

IntelliSense Pre-selects Most Recently Used Members. If this box is checked, IntelliSense will maintain and use a historical list of the most frequently used members for a given type. This “MFU” list is then used to preselect members in a completion list.

Source of Information : Sams Microsoft Visual Studio 2008 Unleashed


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner