Visual studio snippets

I only recently graduated from part-time coder to full-time coder. My previous job was part engineer, part programmer, so I never really spent enough time looking into some of the nicer features of Visual Studio.

Luckily, that’s all changed now. Snippets have by far become my favorite coding shortcut. At first, I didn’t think I had much use for it. If I had to create a long chunk of code I’d just revisit an older project and copy it over, no problems right? Well, now that I’m always coding I see what a waste of time that was.

I’m sure you can find out how to do these just about anywhere, in fact, I’m certain MSDN has a nice section on it, but I’m going to run you through it all the same.

Here’s my favorite snippet, it’s for an IConverter to be used in your xaml. These always have exactly the same starting point, so they’re perfect.

Step one is create a new file with the extension .snippet. In this case, I named it IConverter.snippet.

Now copy all the following code into it and I’ll go through it step by step. I’ve highlighted all the sections you’ll want to change.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="">
  <CodeSnippet Format="1.0.0">
        <Code Language="csharp"><![CDATA[**[ValueConversion(typeof($type1$), typeof($type2$))]
    public class $class$ : IValueConverter
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            if (!(value is $type1$))
                return 0;


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            throw new NotImplementedException();

OK, it’s pretty straightforward from here. Give the snippet a Name and Author, although they don’t really matter too much. More importantly, replace SnippetShortcut with something useful, in my case I used conv. This is the shortcut you’ll use in Visual Studio to run the snippet. All you need to do it type conv[TAB][TAB] and you’re up and running.

In the declarations section,


You can declare all the variables in your code snippet, with an ID and Default Value. The ID can be used in the codeblock below. You have to surround the ID in $, like this $type1$ for the snipper to find and replace it correctly. The default value should be something sensible. I like to try and make that a useable value, so if I forget what that should be, the default gives me a hint.

Finally, replace the section that is highlighted bold (don’t delete the [CDATA[ or $end$ sections or it’ll break). Make sure to replace any section that might be a variable in your code block with the correct $variablename$.

OK, so you’re all done, now what? Well, open up Visual Studio, goto Tools —> Code Snippets Manager, click Add and locate your snippet folder. That’s it.

Now, after all that hard work you can relax and enjoy the beauty of code snippets.


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s