Visual Studio Tip: Using Code Snippets

Posted by: the telerik blogs, on 04 Sep 2008 | View original | Bookmarked: 0 time(s)

A few weeks ago I blogged about creating your own reusable templates for Visual Studio. One of my favorite things about Visual Studio is that it offers a lot of great ways for developers to improve the development experience (and productivity) by adding many extensibility points in the IDE. Now developers have the option of adding on to existing features of Visual Studio with templates and macros, or integrating completely unique features directly into the IDE with add-ins. One of these extensibility features, snippets, adds a ton of value to developer productivity and is incredibly easy to use.

Snippets are small, parameterized code templates. They can save developers time by quickly generating repetitive code such as class definitions, property declarations, or iterations. Whereas before Visual Studio we had to drudge through such repetitive programming tasks, now we can add these bits of code to our projects with just a few keystrokes.

Using Snippets

In order to begin using snippets, all you have to do is type the snippet name and it will appear in Visual Studio's intellisense menu.

1

Once you have either fully typed the snippet name or selected it from the intellisense, simply hit TAB,TAB to fully insert the snippet into your code file. For example, typing foreach and then hitting TAB,TAB will insert a parameterized foreach loop into your code.

2

An alternative method for inserting snippets is to select them directly from a snippet menu. Using the shortcut Ctrl-K, Ctrl-X will open the snippet menu. From here developers can navigate through folders of snippets to find the right one to insert into the code.

3

 

When a snippet is inserted into your code, editable field are highlighted and can be navigated to and changed before the snippet is fully inserted. A change to a field will change all other instances of the field in the snippet. You can navigate the snippet code using the following keys:

  • TAB - Navigates to the next field
  • SHIFT + TAB - Navigates to the previous field
  • ENTER - Finishes editing, returns cursor to its position prior to inserting the snippet

Code Snippet Manager

Visual Studio includes a Code Snippets Manager which can be used to browse snippets by language, as well as add, remove, and import snippets. My next post will cover how to add your own custom snippets, but if you'd like to take a look at what snippets are shipped with the IDE, open the Code Snippets Manager by navigating to Tools | Code Snippets Manager... or typing the shortcut CTRL+K,CTRL+B.

4 

If you'd like to get a head start on my next post and see what makes a code snippet, pay attention to the Location text box at the top of the Code Snippets Manager. When you highlight a snippet, Location will show the directory the snippet's XML file is located. Take a peak inside one or two to get a feel for how a snippet is created.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: Visual Studio | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 422 | Hits: 5

Similar Posts

  • Video: CodeRush Metric Shader Plugin more
  • No JavaScript IntelliSense in VS 2010 Beta 2? Reset your Settings more
  • My History of Visual Studio (Part 10, final) more
  • My ASP.NET MVC stack and why I chosen it more
  • "ADO.NET Entity data model" or "edmx" or "L2E" debug visualizer more
  • Debugger Visualizers not working in ASP.NET Medium Trust more
  • Multi-Targeting Support (VS 2010 and .NET 4 Series) more
  • August's Toolbox Column Now Online more
  • How to Enhance Projects Using MSBuild on a Machine Without Telerik OpenAccess ORM Installed more
  • Visual Studio Short Keys and Some Tips and Tricks more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD