Using Code Snippets

Published Sat, Mar 25 2006 18:54 | Paul June

Visual Studio 2005 offers a great IDE for improving your coding speeds. One if the feature that would definitely boost your speed up is the Code Snippet.

Code Snippets are pieces of code that you can automatically insert in your source code to avoid redundant coding.

To insert code snippets on your code, simply right-click on the editor and choose insert snippet.

You will then be prompted to choose from the library of snippets that you have. If you’re asking on how you could create your own directory of snippets, I’ll be discussing it on a seperate article.

After choosing the snippet library you will then get the list of available snippets for that library. Lets try selecting the “for” snippet.

By selecting a snippet, a code then is automatically generated. Plus the generated code would highlight the thing that needs editing.

Another cool feature of code snippets is its ability to wrap your code with a snippet. For example you have this code:

DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col1");
DataRow dr = dt.NewRow();
...

And then you realize that you must place this in a reverse for-loop. You can do it easily by using “Surround With”. To do this, just highlight the code, then right click and select “Surround With”

Just like using code-snippets, you’ll also be prompted to select the code-snippet to wrap this code.

Let’s try selecting a reverse for-loop.

Voila! Your code now is wrapped with a reverse for-loop statement.

Leave a Comment

Name:  
Website: