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:
and then hit <TAB> – it inserts this chunk of code:
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”:
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:
And then I have this one for creating a new exception class:
And also, here’s a more complicated one I use for creating a new unit test:
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!