How can I change the default Visual Studio C# new class file template?

C#Visual Studio-2010

C# Problem Overview


Is it possible to change the template in Visual Studio 2010 so that the class definition is changed from:

class Class1
{

}

to:

public class Class1
{

}

When creating a new class via Add->Class in the context menu.

I would also ideally like to be able to create a class in one context menu click. I copy+paste existing class files to avoid the file dialog.

C# Solutions


Solution 1 - C#

You could modify the following file:

c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

It contains the template used when you add a new class. Inside the same folder you also have the template for interfaces: Interface.zip so that they are public by default. IIRC a restart of VS is necessary to pick the changes.

Solution 2 - C#

You can create your own template by putting a file in C:\Users\you\Documents\Visual Studio 2010\Templates\ItemTemplates\Visual C#.

For example, you can put "publicclass.cs" with this content :

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
	public class $safeitemrootname$
	{
	}
}

In order to avoid the class dialog, you can use the smart tag. Anywhere you would to use an inexisting class, simply type the class name, and press AltShiftF10 to popout the "generate class" menu.

Solution 3 - C#

This is possible as described here and here.

You might see some issues due to the Template Cache of VS - on how to deal with them see esp. the comments here.

An "official" source on how to do this can be found at http://blogs.msdn.com/b/oanapl/archive/2009/03/06/visual-studio-templates-add-new-item-to-project.aspx

Solution 4 - C#

You have to manually edit the template files of Visual Studio.

See this link for a detailed HOW-TO.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris SView Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow
Solution 2 - C#Steve BView Answer on Stackoverflow
Solution 3 - C#YahiaView Answer on Stackoverflow
Solution 4 - C#ken2kView Answer on Stackoverflow