Press "Enter" to skip to content

Automate your boilerplate classes with Visual Studio templates

I’m working on a program where I’ll need to create 25-50 classes that do the same type of thing and have the same basic structure. Instead of copy-pasting-editing for each class, I’m going to save time and reduce errors by creating a class template for Visual Studio to use for these classes.



Example source code description

The project is a game that connects to Twitch IRC chat.

People in chat type a command, the program recognizes it, and then does the required action. This is the Command Design Pattern, which you can read more about here.


Without a custom class template

In the video, I demonstrate the steps I need to perform when I add a new class (using the default Visual Studio class template) and convert it into an empty class that inherits from the base class and implements the interface required for this program.


For this class, the steps are:

  • Create new class
  • Delete unused “using” directives
  • Change “namespace” to file-scoped
  • Make class public
  • Add base class and interface
  • Implement missing members
  • Rearrange members (CommandName property above constructor)
  • Convert CommandName property to expression-bodied property with value
  • Move “: base(players)” in constructor to separate line
  • Delete throw new exception from Execute()


While it doesn’t take extremely long to do that, it will slow me down as I create the 25 to 50 classes I need for this program.

Plus, each of these manual steps is another opportunity for me to forget or make a mistake.

So, I created a custom class template to use in Visual Studio when creating each chat command handler class.


Creating the custom class template

The steps are simple:


Create the template from the class by following these steps:

  • In Visual Studio menu, select Project -> Export template…
  • Select “Item template” and the project where the template file is located
    • Click “Next”
  • Select the name of the file containing the template
    • Click “Next”
  • (Optional) Select the references used by the class
  • Enter the name of the template
    • This will appear in Visual Studio when you click “Add -> New”
    • (Optional) Enter the description and add an icon
  • Not the output location, where the template zip file will be placed
    • Defaults to OneDrive
    • If you want to share the template with teammates, use a network shared drive


When the template zip file is created, delete the file from your project and restart Visual Studio.


With a custom class template

Now, when you click “Add -> New Item…” in Visual Studio, you should see the name of the template you created. Selecting that, and entering the class file name, will create a new class file with the custom format you created.

NOTE: This isn’t just for classes. You can create templates for interfaces, XML files, JSON files, etc. You can even create templates for complete projects.

    Leave a Reply

    Your email address will not be published. Required fields are marked *