Creating your own snippets for Visual Studio

It came up this week that I have several snippets that I use all the time, which I think are real time-savers. Snippets are chunks of code with insertable elements/tokens that you can use to quickly create a piece of commonly-used code.

For example, if I type “argNull” in a code window:

image

and then hit <TAB> – it inserts this chunk of code:

image

But wait, before you click or type anything, you might notice that “ArgName” is highlighted. That is a “token” that is used in the snippet – if I simply type a name and hit <TAB> or <Enter> (when done), it will update anywhere else that had that “ArgName”:

image

You can also have several of those tokens and can <TAB> (and <SHIFT>+<TAB>) between them, while you are using a snippet. How is this done? Well, for some reason the mechanism to create these snippets is not included in Visual Studio. The good news though is that there are external tools – and free ones.

I’ve been using “Snippet Designer” (https://snippetdesigner.codeplex.com/) or you can get it from Tools –> Extension and Updates and search for “snippet”

Here are 3 snippets I use all of the time – the first two are for quickly adding argument validation to a method:

image

image

And then I have this one for creating a new exception class:

image

And also, here’s a more complicated one I use for creating a new unit test:

image

Bottom line, if you find yourself typing the same code over and over – spend a few minutes and make some snippets and save yourself some time!

Posted in Best-practices, Organization will set you free, Professional Development, Uncategorized, Visual Studio
One comment on “Creating your own snippets for Visual Studio

Leave a Reply

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

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: