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”:
and then hit <tab><tab>:
and type in the argument name, and hit <tab>, then <enter>:
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”:
then hit <tab><tab>:
and type in the argument name, hit <tab>, then <enter>:
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”:
and then hit <tab><tab>:
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:
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”:
and hit <tab><tab>:
then fill in the description, data type, property name and field name, thusly:
and hit <enter> to commit it:
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:
From where can I download the snippets?
Snippets are nothing more than XML files. I have put these out as a gist, available here:
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:
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:
then click on Import:
find the files on your file system:
and a wizard will guide you the rest of the way:
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: