Want to save some time writing code for that next big project? The new code
snippets feature in Visual Studio 2005 will enable the developer to have a
toolset of commonly used code for performing various functions.
The Code Snippets Manager
Next time your in your development project in Visual Studio 2005, check out
the Code Snippets Manager. This tool can be accessed by going to the Tools |
Code Snippets Manager... menu option with Visual Studio 2005.

Figure 1 - The Code Snippets Manager
You'll notice a folder listing broken down into various types of functions
you may want to perform in your application. For example, by clicking on the
Connectivity and Networking folder, you'll find functions like sending an email
message or reading data from a serial port.
There are several buttons available which we'll talk about the obvious one is
Remove which lets you click on a snippet or folder and remove the snippet from
the Manager. In addition you can Add or Import new snippets, and finally search
for snippets available online.
Using a Snippet
Let's go about using one of these snippets.
Place your cursor in your code where you want to place the snippet.
Right click and select Insert Snippet from the context menu. This will bring
up a drop where you can navigate to the code snippet you wish to use. You will
see your path to the snippet being built on your code page as select the item
(see Figure 2).

Once you select your code snippet, the generated code will be placed in your
code, as in the following example:
Dim message As New MailMessage("sender@address",
"from@address",
"Subject",
"Message Text")
Dim emailClient As New SmtpClient("Email
Server Name")
emailClient.Send(message)
The snippet tool will place the necessary code for sending the message, in
addition it will highlight the values that you should change within your
application.
Creating a Snippet
Snippets are formatted XML files that contain your code as well as any other
descriptive information about the code. This XML file has the .snippet extension
and is structured in the following manner:
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>
My Snippet
</Title>
</Header>
<Snippet>
<Code
Language="VB">
<![CDATA[MessageBox.Show("Hello World")]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
You can see the XML file is broken down into a Header section which contains
the title to describe your function, and the Snippet section to contain your
code. Also note that the code is contained within a CDATA section.
Once you have created your snippet, you can now import the snippet using the
Code Snippet Manager as in figure 1 of this article. Just save your XML file
using the .snippet extension, then click the Import button within the Code
Snippet Manager. Navigate to your file and select it in order to import into the
Manager. You should now see your code snippet.
You'll find code snippets are an excellent way of organizing code you
repeatably use within your applications.