Code Snippets in Visual Web Developer and VS 2005

One of the nice new features of Visual Web Developer (which is free) and VS 2005 is the new "Code Snippet Support" that has been added.  This handy feature allows you to quickly find and use code-libraries of common patterns (examples: get/set property definitions, data access code, file IO, exception handling blocks, etc).

To try it out, just right-click within a code file to bring up a context menu:

Select "Insert Snippet" from the context menu, and then choose the category of snippet you want to use (for example: "File System"):

And then the code-nugget to inject:

You'll then have the code added at that location in the editor.  Common variable values will be automatically highlighted for you, with the cursor set on the first value.  You can replace the values with your own, and then tab to automatically jump to fill in the next value (no need to use the mouse or arrow keys):

What makes code snippets really cool is that you can easily create and define your own.  So over-time you can build up your own library of common patterns that you use, and add efficiency to your daily tasks (for example: do you always write data code in a certain way, or insert objects into the ASP.NET cache using a specific pattern, or validate security, etc.)  These snippets are stored within XML files on disk, and can be easily shared across a dev team (or friends).

Update: Several people have pointed out that C# doesn't have as many built-in snippets out of the box as the VB screenshots above.  You can download the same routines (but in C#) to add to VS 2005 or Visual Web Developer here to get them: http://msdn.microsoft.com/vstudio/downloads/codesnippets/default.aspx

Here are a couple of articles and links I found on the web that go into how to build and use your own snippets:

Hope this helps,

Scott

 

5 Comments

  • Hey Scott,



    But i can't use this extensive snippets library in C# right ? Is there a workaround ?



    I get only a handful in contrast to the hundreds in VB.Net



    I did see there was gotcodesnippets.com - but they cant match the inbuilt snippets you guys are providing with VB.Net

  • I don't get that nice menu in C#... Is there an C# version of those usefull looking menus?

  • Hi Grey/Dave -- Thanks to Gavin's comment above, I just posted a pointer to where you can download the same menu of snippets for C#.

  • Snippets are useful, but as a web-developer I would really, really like to be able to make snippets for other languages. A css-snippet that sets up my page with an centered column working in both IE and other browsers, a XHTML-snippet with the markup needed for a three-column lay-out etc.



    Also working with the Atlas preview, which has no intellisense support in ASPX an javascript files, it would be soooo nice to be able to set up a couple of snippets so i didn't have to type



    control = new Web.UI.Control($('tagid'));

    control.initialize();



    over and over !!!!



  • Instead of jumping right ahead and running each VSI file from

    the C# Code snippets download site, you may want to

    prep your Visual Studio first to organize the installation of these snippets.



    Just Add Code Snippets Directories (Application, Collection and Arrays, Connectivity, etc.)

    first for each task-based snippet.



    1. Open Visual Studion 2005

    2. Click Tools -> Code Snippets Manager -> Add button

    3. Decide where to put the C# snippers( I created mine under

    C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033 )



    Note that You can create all directories from here via create icon



    4. Close Visual Studio



    Then Run the VSI files ( Application.vsi ,Connectivity.vsi, etc.)

    You will then be able to place the snippets on the proper directories

Comments have been disabled for this content.