Using Visual Studio 2005 Code Snippets to Write Better Code Faster
page 1 of 3
Published: 29 May 2006
Unedited - Community Contributed
Code Snippets provide simple code generation for repetitive tasks within the Visual Studio 2005 IDE. For example, rather than typing a complete property declaration or case statement, just type a brief shortcut, TAB, and fill in a couple of values. The construct is built for you. In this article, Steve shows how to take advantage of the built in Visual Studio 2005 Code Snippets and how to write your own.
by Steven Smith
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26180/ 35
Article Contents:


In my accompanying article, Visual Studio Debugger Tips and Tricks, I showed a number of ways that properties and classes could be customized with attributes in order to make debugging such classes simpler and more streamlined.  The end result of those attributes was something like the Person2 class shown in Listing 1.

Listing 1

[DebuggerDisplay("Person2: {Name}")]class Person2
  private string _name;
  public string Name
      return _name;
      _name = value;

Clearly there is a lot more code here than before we started adding attributes, and arguably the code is a bit tougher to read with all of this attribute "clutter."  We can fix the latter easily enough through the use of a #region, which can encapsulate the property in a named region (in this case called Name).  The end result is something we can easily collapse so that we are not faced with all this clutter while we are working with our Person2 class.  To do this, you could hand type #region Name and then #endregion, but a much more efficient way is to take advantage of Code Snippets.  In this case, you can simply highlight the lines you would like to place in a #region and right click, select Surround With, and then choose #region.  The #region statements are placed for you and the name of the region is highlighted in green and has focus, such that as soon as you start typing, it will have whatever value you type for it.  Hit enter and the Code Snippet wizard completes, the green highlighting disappears and you are left with your code.

Code Snippets are a fantastic feature of Visual Studio 2005.  If you have not used them yet, make a point to try them out.  There is no limit to the kind of code generation you can accomplish with these template-driven tools.  You can learn more about them, find others and share your own at  Visual Studio comes with a bunch of snippets built in, but you can modify these or create your own as well.  For instance, by default if you type "prop" in a C# class, you will see Intellisense showing prop as a code snippet.  Similarly, "propg" will create a get-only property.  The result of "prop" by default is shown in Figure 1.

Figure 1

As you can see, it defaults to an int, but simply type string and it will change both "int" fields to strings.  Likewise, changing myVar will not only change it on the first line (23), but also in both locations within the get/set accessors (lines 27-28).

Okay, so Code Snippets are cool, there is a bunch built-in and you can make your own, but what does that have to do with the Debugging Tips and Tricks article?  Well, if you decide you would like to always create your properties such that DebuggerStepThrough is specified, the property is in its own #region, and DebuggerBrowsable is set to Never, you can simply create your own snippet or modify one of the existing ones.  You can view all of the existing snippets using the Snippet Manager under the Tools menu or the shortcut, Ctrl-K-Ctrl-B.  The "prop" snippet, for instance, is located under \Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#\prop.snippet.  Snippets are simply XML files, so you can open this and edit it with Visual Studio or Notepad.  However, there are two tools available that are specifically designed for editing snippets.

Editing Code Snippets

The two most popular tools for editing Code Snippets are the Code Snippet Editor for Visual Basic 2005 (which works fine for all languages despite the name) and Snippy, a GotDotNet project for doing the same.  Figures 2 and 3 show the Code Snippet Editor for Visual Basic, including how to use it to edit C# snippets (just add the path to the C# snippets to the TreeView on the left).

Figure 2 - A Read Only Property Snippet in Visual Basic

Figure 3 - A Read Only Property Snippet in C#

Snippy works similar to the VB editor.  Figure 4 shows an example of a snippet in the Snippy editor.

Figure 4 - A Console.WriteLine snippet in Snippy


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-15 3:38:11 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search