A Developer WeBLOG RSS 2.0

Code Snippet is a feature in Visual Studio that allows you to have a predefine code, which can be inserted anywhere in your code. It sorts of like a macro, where you can just write the macro name anywhere in your code file and then press the Tab button to replace it with the piece of code you've defined. This feature was first introduced in Visual Studio 2005.

I have to say that I love code snippet. It has saved me huge amount of time. Just about any line of code that I would repetitively write, I put them into a code snippet. One of the cool thing with code snippet is that, it is supported by intelisense. This means, you don't have to type the whole snipet name everytime you want to use it. Just type a the first few characters, select your snippet from intelisense, and then press Tab twice. There you go, you have instant code generation, without having to write any codes.

Code snipet is defined in an Xml document, which has to be named with .snippet extension (e.g. MySnippet.Snippet). Code snippet consists of 2 parts: the header part, and the snippet part. The Header part contains metadata of the snippet, things like the title, shortcut name, description, author, and the types of the snippet. The snippet part contains the actual code that you would like to define in the code snippet. The code snippet itself must be inside a CodeSnippets Xml Node. Here is an example of a code snippet file (The first code snippet is just a template, the second one is an example of a working snippet):

Once you have created your snippet file, you need to place that file inside the "My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippet" folder. Or, alternativelly you could use Visual Studio Snippet Manager to define a new folder to put all your snippet files (Tools -> Snippet Manager).

One cool thing that you can do with code snippet is to define parameters for your code snippet. This allows you to easily changes things inside your code snippet. Lets take the example snippet on the picture above as an example. Rather than hardcoding the string value ("FOO"), we could just create a parameter for that. If you have a snippet with parameter, once your snippet code is written out, the cursor will automatically highlight your parameter so that you can replace the value of your parameter straight away. Moreover if you have multiple parameters, you can use the Tab key to jump from one parameter to another parameter.

To define a parameter, you need to add a literal declaration inside your snippet. In your literal declaration, you need to define an ID for the snippet parameter, which can be used later on inside your snippet code. Lastly, to use a snippet parameter, in the snippet code, you must reference the paramater ID encapsulated with the dollar sign (e.g. $ID$). That's it, thats all you need to do. An ID is the minimum value that you need to have to create snippet parameter, but there are other things that you can define for your snippet parameter, such as: a default value for the parameter and a tooltip value for the parameter. Applying parameter to the snippet example, the final snippet looks like this:

Another cool thing that you can do with code snipet is that, Instead of plain inserting a code, you are able to surround a piece of code with your snippet code. To do this all you need to do is to define a snippet of type "SurroundsWith", and then you need to define the area to place the target code by using the $selected$ value. To use your surrounds with snippet, you have to select a piece of code that you want to surround, then press cntrl+K and cntrl+S, and select the snippet that you want to use. Please note that, you can not use the "surrounds with" snippet by using intellisense. Here's an example of a "SurroundWith" snippet (taken from the default "IF" snippet):

Thats it, I hope you find code snippet to be as usefull as I find it to be.

RWendi

Wednesday, November 05, 2008 8:59:09 AM UTC |  Comments [0]
c# | VS2008
All Content © 2010, RWendi