Dynamic code generation has existed for quite a while. In fact, you’ll find tools that can help developers generate tens of thousands of lines of code with just a click of a button. Some of these are even flexible enough to allow you to modify the tokens in the template—or even the templates themselves—for added control over the output of the code. (The Code Generation Network Web site has an extensive list of these tools, quite a few of which I have used in the past—some with great success.)
I have always wanted to have the ability to create code inline without loading an external tool, or loading a Visual Studio add-in. That is where code snippets come to the rescue. They are not as powerful as some of the generators, but they perform more work in the day-to-day coding that experienced developers do.
Visual Studio 2005 IntelliSense code snippets enable you to create your own IntelliSense snippets without leaving the Visual Studio IDE. This ability boosts productivity by allowing you to create and insert samples of code that you use regularly. It also allows custom code developers to provide snippet examples of their own libraries and APIs.
Table 1 shows the current list of included snippets as of this writing.
Table 1: List of Current Visual Studio 2005 IntelliSense Code Snippets
Name | Description | Valid Locations to Insert Snippet |
---|---|---|
#if | Creates a #if directive and a #endif directive | Anywhere |
#region | Creates a #region directive and a #endregion directive | Anywhere |
~ | Creates a destructor for the containing class | Inside a class |
attribute | Creates a declaration for a class that derives from Attribute | Inside a namespace (including the global namespace), a class, or a struct |
checked | Creates a checked block | Inside a method, an indexer, a property accessor, or an event accessor |
class | Creates a class declaration | Inside a namespace (including the global namespace), a class, or a struct |
ctor | Creates a constructor for the containing class | Inside a class |
cw | Creates a call to WriteLine | Inside a method, an indexer, a property accessor, or an event accessor |
do | Creates a do while loop | Inside a method, an indexer, a property accessor, or an event accessor |
else | Creates an else block | Inside a method, an indexer, a property accessor, or an event accessor |
enum | Creates an enum declaration | Inside a namespace (including the global namespace), a class, or a struct |
equals | Creates a method declaration that overrides the Equals method defined in the Object class | Inside a class or a struct |
exception | Creates a declaration for a class that derives from an exception (Exception by default) | Inside a namespace (including the global namespace), a class, or a struct |
for | Creates a for loop | Inside a method, an indexer, a property accessor, or an event accessor |
foreach | Creates a foreach loop | Inside a method, an indexer, a property accessor, or an event accessor |
forr | Creates a for loop that decrements the loop variable after each iteration | Inside a method, an indexer, a property accessor, or an event accessor |
if | Creates an if block | Inside a method, an indexer, a property accessor, or an event accessor |
indexer | Creates an indexer declaration | Inside a class or a struct |
interface | Creates an interface declaration | Inside a namespace (including the global namespace), a class, or a struct |
invoke | Creates a block that safely invokes an event | Inside a method, an indexer, a property accessor, or an event accessor |
iterator | Creates an iterator | Inside a class or a struct |
iterindex | Creates a “named” iterator and indexer pair by using a nested class | Inside a class or a struct |
lock | Creates a lock block | Inside a method, an indexer, a property accessor, or an event accessor |
mbox | Creates a call to System.Windows.Forms.MessageBox.Show (You may need to add a reference to System.Windows.Forms.dll.) | Inside a method, an indexer, a property accessor, or an event accessor |
namespace | Creates a namespace declaration | Inside a namespace (including the global namespace) |
prop | Creates a property declaration and a backing field | Inside a class or a struct |
propg | Creates a property declaration with only a “get” accessor and a backing field | Inside a class or a struct |
sim | Creates a static int Main method declaration | Inside a class or a struct |
struct | Creates a struct declaration | Inside a namespace (including the global namespace), a class, or a struct |
svm | Creates a static void Main method declaration | Inside a class or a struct |
switch | Creates a switch block | Inside a method, an indexer, a property accessor, or an event accessor |
try | Creates a try-catch block | Inside a method, an indexer, a property accessor, or an event accessor |
tryf | Creates a try-finally block | Inside a method, an indexer, a property accessor, or an event accessor |
unchecked | Creates an unchecked block | Inside a method, an indexer, a property accessor, or an event accessor |
unsafe | Creates an unsafe block | Inside a method, an indexer, a property accessor, or an event accessor |
using | Creates a using directive | Inside a namespace (including the global namespace) |
while | Creates a while loop | Inside a method, an indexer, a property accessor, or an event accessor |
Take a Shallow Dive
To access code snippets, simply right-click in a code window and click on ‘Insert Snippet’, as shown in Figure 1.
Figure 1: Access Code Snippets
Depending on the type of file, you then will see the types of snippets that are available to you. This example presents a C# class file, so you see two options: Office Development and Visual C#, as shown in Figure 2.
Figure 2: Available Types of Snippets
The category you select displays a list of its available snippets. These snippets will vary; you add to them or modify the existing ones. The list in Figure 3 is from the Visual C# category.
Figure 3: Available Snippets in Visual C#
The snippet name that you select dictates the code that is injected into the code window and at which location. For this C# example, choose ctor, which is the snippet to create a default constructor for your class (see Figure 4).
Figure 4: The ctor Code Snippet Creates a Default Constructor for Your Class
Clicking on the label injects the code into the spot where your cursor was when you first right-clicked in the code window. Figure 5 shows the code.
Figure 5: The Injected ctor Code
Now you can explore the ctor.snippet file that is located in the folder C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#. Figure 6 shows this snippet file.
Figure 6: The ctor.snippet File
The file has only one code snippet defined, but it can have as many as defined in the schema. The title element will be displayed in the IntelliSense picker as well as in the Code Snippets Manager (see Figure 7). The manager also shows the other header elements; the Code Snippets Manager window displays the description, shortcut, snippet type, and the author.
Figure 7: Title and Header Elements in the Code Snippets Manager