Custom New Class Template
Visual C#

Custom New Class TemplateVisual C#

The Easy, Incorrect Way

When you add a new class in Visual C#, it is filled with a template.  You can change this.

Step 1.

Go to this folder (for VS2019):

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

 

Step 2.

Edit this file in Admin mode (so that you can save it):

Class.cs

Use the variables within this template to your liking.

 

Example

This is not perfect, but allows me to see the date and time, and edit it later as I please.

// Jason Allen Doucette
// MONTH DAY, $year$ - $time$
// $itemname$

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace $rootnamespace$
{
	class $safeitemrootname$
	{
		// ---- properties

		// ---- data members

		// ---- methods

	}  // class $safeitemrootname$
}  // namespace $rootnamespace$

See what I did there?

At the end of the file, when you are editing a function at the very end, it is annoying to see three } at the end, and I often add comments like this.  It is nice to have this added automatically (though it can be a pain if you rename namespaces and classes and do not update comments at the same time).

It may also be nice to automatically have proper includes, a default constructor, along with common functions you almost always make.  Deleting extra code is easier than writing it every time.  YMMV.

 

Data Loss Warning

Make sure you save a copy of this as some Visual Studio updates will overwrite your changes.

Thus, you should use…

 

The Hard, Correct Way

Create

Please follow these directions: the correct way to do this.  It works, but it is cumbersome.  Be sure to name the template “Class” so when you later add an item via “Add, Class”, it will automatically use your new template, and not the original “Class” template.

The resultant files for your new template are added to a Class.zip file, defaulting into the following folder:

%USERPROFILE%\Documents\Visual Studio VERSION\My Exported Templates
E.g.
C:\Users\%USERNAME%\Documents\Visual Studio 2019\My Exported Templates
C:\Users\%USERNAME%\Documents\Visual Studio 2022\My Exported Templates

 

Visual Studio will then default-ly automatically import the template by copying Class.zip into the following location, to be imported the next time you restart Visual Studio.

%USERPROFILE%\Documents\Visual Studio VERSION\Templates\ItemTemplates
E.g.
C:\Users\%USERNAME%\Documents\Visual Studio 2019\Templates\ItemTemplates
C:\Users\%USERNAME%\Documents\Visual Studio 2022\Templates\ItemTemplates

 

Edit

If you want to edit it, you need to go hack the files yourself in a .zip file, and then re-zip it, as explained here.  Yes, that’s right.  This is the correct way.

Make sure you either edit both Class.zip files, or at least the one in ItemTemplates folder, since the one in My Exported Templates is not used.

 

Version Update Warning

Of course, when you update to VS2022, this is not maintained, and you will have to repeat.

 

Jason Doucette Avatar

Leave a Reply