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:
- http://aspnet.4guysfromrolla.com/articles/122105-1.aspx
- http://www.developer.com/net/vb/article.php/10926_3505156_1
- http://blog.davidyack.com/archive/2006/01/26/3884.aspx (David's blog has a whole list of great links here)
- http://blog.davidyack.com/archive/2006/01/27/3886.aspx
- http://aspalliance.com/752
Hope this helps,
Scott