One of the tools, Resharper is providing, is called Code Templates. Those templates help the developers write common code much faster. There are many different types of code templates:
- Live templates
- Surround templates
- File templates
- Postfix templates (only for C#)
- Source templates (only for C#)
All these type of templates offer different ways to quickly add or manipulate a piece of code. Well… you have to try it, to know how powerful it really is.
In this short blog, I will zoom into Live templates. Maybe you have already tried to use Visual Studio code snippets? Resharper Live templates is kind of the same – just a lot smarter and a lot better.
A skilled ex-colleague of mine, introduced this tool to me by describing it as Visual Studio Code Snippets on coke. You can use existing Live templates, provided by Resharper, and you can even make your own customized templates.
To use an existing template, you need to have Visual Studio installed along with Resharper.
Follow these steps to create a file with a constructor:
- Create a new project of any type
- Create a new class file in the project. Give it any name you like.
- Put your cursor inside the curly brackets of the class.
- Type “ctor” and click enter.
- Pow… hopefully a small piece of code: an empty constructor, appeared in your class.
Resharper offers a lot of pre-built templates, for C#, XML, HTML, VB.NET and much more. To check them out, click the “Resharper” tab in your main navigation in Visual Studio, then click “Tools” and finally click “Template Explorer”.
Creating a Template
You can also create your own templates. I have created several templates, for standard code, I found myself writing again and again. One of the most simple examples would be my template for an async unit test.
To create this, follow the steps:
- Open Resharper Template Explorer in Visual Studio by clicking the “Resharper” tab in your main navigation in Visual Studio, then click “Tools” and finally click “Template Explorer”.
- Click the small icon representing a new template.
- Add the following text in the template file:
public async Task $Scenario$_$ExpectedOutcome$()
- On your right side, you can enter the shortcut and the description of the template. Provide the values. shortcut = testasync, description = Async unit test
- Save the new template, by clicking CTRL+S
- Navigate to any class file, and try the new template by typing the shortcut (“testasync”).
- Notice you can enter a value for the scenario, and then hit ENTER, to move to the part called ExpectedOutome. Again type a value, and hit ENTER, and notice the cursor moving to the point, where you set the protected value $END$ in the template.
- HAPPY CODING.
Now you should be ready to make any template of your own, to save valuable time, avoiding to write the same code again and again, and have more focus on interesting parts of writing software.:)
Please leave a message, if you have any issues on my guide, and I will improve it accordingly. You can read more about Resharper Templates here: https://www.jetbrains.com/resharper/features/code_templates.html