Share via


C#: Customise Add New Item Template

The aim of this article is to introduce the reader to the idea of customizing the templates Visual Studio uses as you choose Add > New Item in Visual Studio.


Background

A recent MSDN forum question asked, "How do I make all my new Classes public by default?".  This article explains how to do that, but in the process illustrates how you would go about making other similar changes.  As the title implies, when you choose to Add a new Item to a project, a template is used to create the stub which you end up looking at in the edit window once Visual Studio has created it.
Maybe you often find yourself adding a number of standard "using" statements at the top of your classes. You could add those into your standard template.
So much of this article is specific to C# that it is in the C# category but there are templates for all languages and new items.

Method

Like a lot of customization you can do with Visual Studio, this is documented. There are, however, so many things you can customize that many people never manage to wade through the rest of them to find this particular subject.
There is a template file for each of the types of item you can add.  All those new options you see each have their own template file.
Before you get started, make sure you closed all instances of Visual Studio. It's unlikely to have any significant downside if you leave one open but it could be confusing if your changes don't appear to work.
You need to obtain a template for your file. You can go get the contents from one of the templates.

Template Location

The two locations we're interested in for VS2013 on  a 64 bit Win 8.1 machine are:

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

and

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class

These will, obviously, be slightly different on a 32-bit machine or with different versions of Visual Studio. 
If you want to be particularly cautious, it is that last one - ItemTemplatesCache which will be used immediately and you could try with just that and see if you like what you get.  Keep the ItemTemplates version unchanged. ( Chicken noises ).
In that folder, you will see class.cs. You can Edit it and take a look at the contents in Visual Studio.
Either clipboard them out that file or grab the code below:

New Template

Prefixing the class with public is just a matter of typing it into the very obviously appropriate place:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
    }
}

OK,  this is a pretty simple baby-steps kind of a change but you can see that you could just as easily add a constructor stub or a few using statements.
Open up Notepad, paste in either the above or the contents you just got from a template file. Edit to taste.
Save that file somewhere as a txt file in a location you will be able to find it easily.
Close NotePad.
Rename your txt file to class.cs.
The reason you're working this way is you can't create files in program files. Scout's honor - you can read more about that in this article.
Clipboard your new file by selecting and pressing Ctrl+C or leave an instance of File Explorer open with it visible in there if you prefer drag and drop.
Delete one or both of the originals and copy your new class.cs file into their old location(s).

Try it

You closed all instances of Visual Studio right at the start of the process so start it up again. Either open up a new Solution or one you have already and right-click the project, choose Add > Class.  Accept the default name and a brand shiny new class stub should appear in the editor. It will be marked as public.

See Also

C# Portal

Other Resources

Customising Project and Item Templates MSDN