Creating A ReSharper Code Template

ReSharper from JetBrains is a great tool when it comes to productivity optimization when developing software in C#. Although I use it daily, I haven’t thought about customizing it in any way. The time has now come to further extend the usage of ReSharper creating a ReSharper template.

If you haven’t tested ReSharper yet, I highly recommend you to try the trial version. I am pretty sure once you know how to use it, you won’t want to miss it anymore.

What is a ReSharper template?

Using ReSharper (live) templates enables you to generate small pieces of code. This means that you can for example just type ctor and you get a complete constructor in your class. This way, you don’t need to type everything on your own. This remarkably improves your coding speed.

The official website describes them as “far more intelligent version of Visual Studio’s code snippets”.

Why should we create our own templates?

ReSharper comes with many templates which help to improve our productivity. If you know some of them, make sure to open the Template Explorer to further investigate which templates exist and how they can save your time.

Although there are many predefined templates, there is the case that you need a custom template. For this article, I am going to work with a sample from a view model property.

We develop our software using a framework implementing INotifyPropertyChanged in a base class. In our property, we want to call the method NotifyOfPropertyChange to do that work.

This code snippet looks pretty simple and straight forward. But if you have to type it frequently, it can be really annoying. Therefore, creating a custom template is the way we want to do it.

How to create a ReSharper template?

Creating a custom ReSharper template is very simple. ReSharper comes with a Template Explorer which you can find in the ReSharper menu integrated into Visual Studio.

Open the Template Explorer and create a new template using the New Template button in the toolbar. The dialog box collecting the header information is pretty simple. Choose the shortcut you want to type to generate the code template.

After defining the shortcut code, you can start creating your template. You can find a good documentation on the vendor’s website. In this article, I am going to explain what we need in order to create a template for the code example above.

Template definition

The following code defines the template used to generate the view model property described above. Just note that the $-sign is used to create variables. These variables will be exchanged with information during code generation.

Variable definition

$PropertyName$ will contain the name of the property and is defined as a string variable. In our case, we define string as the default data type.

$ValueType$ will contain the data type of the property. In our example, we define Text as the default name.

$FieldName$ will contain the name of the field which should be starting with a lower case letter. This is achieved by using the predefined macro called “Value of another variable with the first letter in lower case”. This macro will be applied to the PropertyName variable.

We use the same variable more than once in our code template. Using the combo box on the right side, we can specify which occurrence of the variable we want to fill in when executing the code template during software development.

Executing the code template

During development, we can simply type vmprop in our Visual Studio code editor. ReSharper will then execute the code template and generate the requested code for us.

Conclusion

ReSharper code templates are very useful. There are a few predefined code templates which help a lot. At some point, we need to extend the functionality by creating a custom code template to further improve productivity.

When we hit this point we can implement our code templates using the Template Explorer in a very simple way.

Let me know if you have already implemented custom code templates and what code they generate for you. It would be interesting to hear about experiences and how far some of you go in using ReSharper.

Claudio Bernasconi

Claudio Bernasconi is a professional software developer, Microsoft certified technology specialist, blogger, technology enthusiast and founder of CarParking Schweiz. Since 2010 he works for KMS AG in Lucerne, Switzerland.