With a combination of hotkeys or via the context menu you can insert pieces of code into your code file, called Code Snippets. They can be used to insert entire methods and or classes, but are used more often to insert commonly used code blocks like try-finally or if-else. As long as the Code Snippet is kept short and reusable it’s all good.
The Code Snippet I use all the time is the one for auto-implemented properties. Try it for yourself. Just type in your code window: “prop” and press Tab, Tab. And you should see the following:
This is called an Insertion Snipped and was added by typing, which is my preferred way of doing it. You can also click the context menu item Insert Snipped and then choose the “prop” item from the list.
Also notice how “int” and “MyProperty” have a yellow background (“int” is blue because it’s selected in the image). These are Replacement Parameters. It doesn’t make much sense in this code snippet, but check out the snippet for a full property (type ‘propf’ and press Tab, Tab):
If you would change ‘int’ to ‘string’ you’d see all instances of ‘int’ change to ‘string’. Same goes for ‘myVar’, if you’d change this to ‘_myVar’ you’d see all instances of ‘myVar’ change to ‘_myVar’. Are you starting to see how Code Snippets can increase your productivity?
You also have Surround-With Code Snippets (C# and C++ only), which works by selecting a line of code and then selecting Surround With… in the context menu and then selecting the code snippet you want from the list. Interesting right?
Want to create your own snippets? Well, you can!
Notice the Import button in the Code Snippets Manager (Tools/Code Snippets Manager). With this button you can import in your Visual Studio the file which contains your Code Snippet (or snippets). Code Snippet files are basically XML files. At its most basic a code snippet looks like:
So what you do to create your own snippets is:
- You add a XML file in Visual Studio
- You copy/paste the xml shown above
- Write in between the Title elements “Write stuff to the Console”
- As value for the Language property of the Code tag fill in “csharp”
- Add some code to CData to make it look like:
- Save it as CustomCodeSnippets.snippet
- Now go to Tools/Code Snippets Manager and click the Import button
- Select the snippet file CustomCodeSnippets.snippet you just created at the location you saved it
- Select My Code Snippets from the Import Code Snippet dialog, so the snippet gets saved at: %USERPROFILE%\Documents\Visual Studio 2013\Code Snippets\Visual Basic\My Code Snippets
- You can test the snippet by selecting Insert Snippet… from the context menu, then selecting My Code Snippets (as that is where we imported it) and selecting “Write stuff to the console”.
You can add an Author, Description and Shortcut element to the Header element. Author and Description is to provide information in the Code Snippets Manager, but the Shortcut element is to make it possible to type a Code Snippet (remember the “prop” shortcut).
You can also add References, Imports and Replacement Values to your snippets. A walkthrough on how to do all these things is provided by Microsoft here: https://msdn.microsoft.com/en-us/library/ms165394.aspx. If you are like me and want to learn by cracking open existing snippets, just go to your Code Snippets Manager and click on any folder or snippet name to check out the location. You can open these snippet files in any editor because they are simply XML files (which should be clear by now).
Happy snippet building!