Common Visual Studio snippets I use

This has come up several times both with other people, and with myself – when I move into a new workstation, I need my snippets, but I don’t have them in one place. So, this blog post will serve as the home of where I keep snippets I use very commonly. These are all easy to use and save a few seconds per-use.

First – I’ll describe each one and then afterwards, I’ll point to the source of where these are and how you can add them to Visual Studio.

Snippet – argNull:
ArgNull simply checks if an argument is null. If it is, it throws an ArgumentNullException. You use it by typing “argNull”:

image

and then hit <tab><tab>:

image

and type in the argument name, and hit <tab>, then <enter>:

image

 

Snippet – argString:
ArgStrng works similarly. This checks an argument for null or empty, and if it is, throws an ArgumentException with an appropriate message. You use it by type “argString”:

image

then hit <tab><tab>:

image

and type in the argument name, hit <tab>, then <enter>:

image

 

Snippet – exceptionProper:
ExceptionProper makes a “proper” exception class. That is, it has all of the constructor overloads you are supposed to have, and it has basic XML code comments so that you’ve have basic intellisense while using the class. To use this snippet, type “exceptionProper”:

image

and then hit <tab><tab>:

image

Now, this is set up to create exceptions for the root of namespace, because that is probably the most-common thing you’d do. You can obviously change the XML code comments later, but this at least gets you in the ballpark. Note that the exception name itself will be based off of “Name”, and it will add the “Exception” suffix to it. Generally speaking, this exception should inherit from the root exception for your library, but if this is the root one – then the “Parent” can just be “System.” which would look like this:

image

Snippet – propProper:
Lastly is propProper. This creates a property with a backing field – but also does two other things. First, it puts the backing field WITH the property, and it has a proper XML code comment. To use this, type “propProper”:

image

and hit <tab><tab>:

image

then fill in the description, data type, property name and field name, thusly:

image

and hit <enter> to commit it:

image

Now, why is the private backing field tucked-up underneath the property? That is so they stay together. Look what happens when I roll-up the property:

image

From where can I download the snippets?
Snippets are nothing more than XML files. I have put these out as a gist, available here:

https://gist.github.com/RobSeder/6f9a5ac1d67144dbff68

Simply download each one and save them as .snippet files somewhere on your hard drive, for the moment. If you can work with .tar and .gz files (like if you have 7zip installed), on the right side of that gist page, you can download all of the files, .zipped and tarred:

image

Otherwise, you can just click “Raw” and then “Save As” in your browser to save each one, individually.

How do I install the snippets for Visual Studio (any version)?
The good news here is that snippets are pretty universal and have been unchanged since the dawn of Visual Studio (as far as I know). So, snippet files work on all version of Visual Studio! I’ve confirmed these with VS2010, 2012, 2013, and 2014 CTP (as of this writing).

Perhaps the easiest way to add these to Visual Studio is to click on the Tools menu, then Code Snippets Manager:

image

then click on Import:

image

find the files on your file system:

image

and a wizard will guide you the rest of the way:

image

How do you make your own snippets?
If you want to change these or make your own, see this blog post. This explains more on how they work, the add-in that you need, and how to create your own:

https://blog.robseder.com/2013/09/29/creating-your-own-snippets-for-visual-studio/

Have fun!

Posted in Best-practices, Organization will set you free, Professional Development, Uncategorized, Visual Studio
3 comments on “Common Visual Studio snippets I use
  1. Binoj Daniel says:

    Pretty good, I noticed that you have not provided any description for the snippet description meta.

    Like

    • Robert Seder says:

      Good catch! I updated the Gist with descriptions and a “help URL” which leads back to this post. I’m not sure how that works. It worked exactly one time, but then I couldn’t reproduce. I started to type “argNull” and hit F1 and it brought up a browser to this post. But after that, it never worked again!

      Anyhow – I updated the Gists though to at least point to here and give some description. thanks!

      Like

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: