Tip: Using Code Snippets in Visual Studio – Boredom Challenge Day 20

Standard

Code Snippets feature of Visual Studio allows us to save pieces of code in snippet files, and then easily put them to our projects with the press of a few shortcut keys. This is useful if there is a specific code that you have to use again and again in different projects (for example, the code that handles Facebook login), so instead of writing it each and everytime (or opening another project and then pasting from there) we can just use the code snippet. This is also useful if you are giving presentations about a technical topic since you can prepare the code you’ll use beforehand and don’t bother to write them from scratch during the presentation.

If you have Visual Studio open now, try it yourself: Press Ctrl+K, X; then select a snippet and press enter. The related code will be automatically added to that line.

1

Under “Documents\Visual Studio 2012\Code Snippets” folder, you’ll see that there are folders for different programming languages, each with a My Code Snippets subfolder where you can put your custom code snippets. If you also wish, you can go to Tools -> Code Snippet Manager in Visual Studio and add other code snippet folders.

2

3

Code snippets are defined in files with .snippet extension and they contain xml code with the following structure:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        Code Snippet Title
      </Title>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[//Your code here]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

So, if we wanted to make a snippet for using a MessageDialog, it would look like this:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        Message Dialog
      </Title>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[MessageDialog messageDialog = new MessageDialog("Enter your message here.");
            await messageDialog.ShowAsync();]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

If we save this into a .snippet file and put it under our “My Code Snippets” folder (or any other folder defined for Visual Studio), we can see and use it like this:

4

5

6

Also keep in mind that we don’t have to create a .snippet file for every single code snippet. We can define multiple code snippets in a single file, like this:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        Code Snippet 1
      </Title>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[Code 1]]>
      </Code>
    </Snippet>
  </CodeSnippet>
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        Code Snippet 2
      </Title>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[Code 2]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Useful, isn’t it?

Advertisements

Comment

Fill in your details below or click an icon to log in:

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