Thursday, January 29, 2009

Code Snippets in Visual Studio

 

Code Snippets in Visual Studio

Recently I found myself writing a ton of VB code however I am working on a new gig and it requries me to switch over to C#.  The tedeous task of remembering to type "foreach" or not typing "dim" has caused me much frustration.  A little tip that will save time and effort and possibly help re-wire my brain so that I can be more efficient is to use Code Snippets.  Code Snippets in VS 2005 or VS 2008 is really one of the coolest things to make you productive.  To use snippets check out the following straight from the Tips and tricks on MSDN.

http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic3

Code Snippets

Code snippets are one of the best productivity features introduced in Visual Studio 2005. It allows you to quickly insert fragments of code to avoid tedious typing (such as typing a for loop) or to give you a template of how to accomplish a certain task (such as sending data over the network). Most of the built-in C# snippets are of the first type – they help you in minimizing repetitive typing, while most of the built-in VB snippets are of the second type – they let you code up a specific task more easily.

There are two ways to insert a snippet. You can type the snippet's alias in the code editor and press Tab twice (you only need to press Tab once for VB) to insert the snippet immediately. After the code snippet has been inserted, you can press Tab and Shift+Tab to jump to different fields within the snippet. This allows you to quickly change the parts of code that need to be modified. Notice that in C#, code snippet aliases also have IntelliSense. You can tell that an item is a code snippet in the IntelliSense list by its snippet icon.

Figure 8. IntelliSense fully supports code snippets

If you don't remember your code snippet's alias, you can also insert it by pressing "Ctrl+K, Ctrl+X" within the code editor or do a mouse right-click with the mouse and select Insert Snippet.... This shows the code snippet picker, which enables you to browse all the snippets that are applicable to your current programming language and to choose the one you want to insert. This method of inserting code snippets works for both C# and Visual Basic. Visual Basic users have yet another way to insert snippets: you can type the first few letters of a snippet alias, followed by "?" and pressing Tab. Visual Studio will display an alphabetical listing of all the code snippet aliases with the one most closely matched highlighted. This feature is available only for Visual Basic users.

Click here for larger image

Happy coding!

Posted: Thursday, January 29, 2009 2:42 PM by Karimka

Coding Times of a .Net Developer from Yard : Code Snippets in Visual Studio

No comments:

Blog Archive