Creating and using code snippets
One of the features that I really appreciate in Visual Studio 2005 is "code snippets". Code snippets help you to speed up the development by giving you shortcuts to frequently used code.
For example if you want to create a for loop, you simply type for and press the "TAB" button twice.

Visual Studio will then automatically generate the code for you as you can see below. 
By using the tab key, you can navigate to the marked words which enables to specify your own names.
That's not all. The nicest thing about code snippets is that you can create your own code snippets.
In the following example, I'll show you how.
- Create a new file cend.snippet within Visual Studio 2005 and save it under the ...\My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets folder.
- Copy the following code in the file, I'll explain the code in a minute:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0">
<Header>
<Shortcut>cend</Shortcut>
<Title>End console</Title>
<Description>Code snippet for ending a console application</Description>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>EndMessage</ID>
<Default>Press any key to continue ... </Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
Console.WriteLine("$EndMessage$");
Console.ReadLine();
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets> - Save the file and open a C# application. In one of the .cs files, type cend which will popup the intellisense menu.

- When you press the "TAB" key twice, the following code will be generated:

- How does it work:
The second line in the snippet file informs Visual Studio to use an xsd to help you by providing intellisense (the xsd is located in C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas\1033\snippetformat.xsd depending on where you installed VS):
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> - The shortcut contains the shortcut that you can use to reference to your code snippet. You can also reference the code snippets via the menu or via the Ctrl+K, Ctrl+X combination.
- A literal is a variable that can be used within your code snippet. When using the code snippet, each literal is a placeholder for user input. You can add a default value as done in the sample.
- Within the code element, comes the real code snippet. It is embedded in a CDATA element to be able to include special elements in the xml document.
- It is possible to create multiple snippets (1 snippet per file).
6 comments:
thanks for this great post , waiting for more ;)
Het is altijd goed om te weten hoe dingen nou under the hood werken en hoe ze in elkaar steken. Na je eerste snippet met de hand in elkaar te hebben gehackt kun je kijken naar Snippy, de snippet tool:
http://www.gotdotnet.com/codegallery/codegallery.aspx?id=b0813ae7-466a-43c2-b2ad-f87e4ee6bc39
Hi Geert,
That was a smart piece of info. Thanks for sharing.
-Navdeep
"It is possible to include multiple snippets in one file."
Nope. Sorry. Doesn't work.
Thanks for the comment Brian. I've changed it.
Keep up the good work.
Post a Comment