What are Code Snippets in Visual Studio


Code Snippets are ready-made snippets of code that can be inserted in your code. They improve the developers efficiency and accuracy because they reduces typing and help code reuse.

There are a number of ways to insert code snippets.

  • The fastest way of course is using the shortcut keys CTRL + K, CTRL + X.
  • Using the Snippet Picker by selecting the Insert Snippet from the source code context menu or the Edit > IntelliSense menu.
  • Or just type the code snippet's shortcut and press tab twice. The shortcuts are displayed in the Completion List window with a torn page icon.

The Code Snippet Manager can be used to add or remove folders containing code snippets, as well as import a snippet to My Code Snippets folder and search online using MSDN with the content type set to IntelliSense Code Snippets. The folders displayed on the left are file system folders, and the snippets in them are XML files with the .snippet extension. To the right are the following details of the selected snippet:

  • Description: A short description of the snippet.
  • Shortcut: The abbreviation displayed in the Completion List windows and used to insert the snippet.
  • SnippetType: This could be one of three types:
    1. Expansion: Inserts the snippet code where the cursor is.
    2. SurroundsWith: Surrounds the selected code with the code from the snippet.
    3. Refactoring: Used in refactoring.
  • Author: The name of the author.

Microsoft provides extra code snippets for C#, the setup file can be downloaded from:
http://msdn.microsoft.com/en-us/vstudio/aa718338.aspx.