Inserting snippets in C#

In Visual Studio 2010 there are a lot of built-in snippets available to the developer. The IDE also allows you to create your own snippets but today I wanted to talk about accessing the built-in ones.

Microsoft provides you with snippets which are pre-written pieces of code that developers tend to use on a regular basis and when dropped in can save you time. Some are just placeholders that you have to modify (like the #region snippet) whereas others act as wrappers of code (like the try-catch block)

Snippets can be accessed on the design page (where you will receive snippets of HTML and ASP.NET code) or from the code behind page where you can get C# snippets along with snippets for developing tests and for working with Microsoft Office products.

You can access these snippets in several ways:

  • Set the cursor on the page where you want the snippet, right-click and select Insert Snippet
  • Set the cursor on the page where you want the snippet and hit CTRL+K, CTRL+X, The same dialog box as above appears.
  • Set the cursor on the page where you want the snippet and on the Menu Bar browse to Edit–>Intellisense–>Insert Snippet
  • Set the cursor on the page where you want the snippet and type the name of the snippet you want (eg #region) and hit the Tab key to get the snippet

 Microsoft also offers the Surround With option which I will explore in another post.

Advertisements

About Ken Cenerelli
I am a Programmer Writer, .NET Developer, Microsoft MVP - Visual Studio and Development Technologies, Public Speaker, Blogger, and Microsoft Azure Nerd. I blog regularly at kencenerelli.wordpress.com and can be found on Twitter via @KenCenerelli.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: