59

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.

Chris S
  • 64,770
  • 52
  • 221
  • 239

4 Answers4

70

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.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 6
    Thanks for providing an answer instead of a link that may break in the future. – DOK Jan 05 '12 at 16:22
  • 2
    I had to also change the files in `C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip` (that's where it unzips them) to get VS to recognize it. – Chris S Jan 06 '12 at 10:08
  • I have been searching for a way to do this for some time. Thank you! – Rodolfo Neuber Apr 25 '12 at 17:01
  • 2
    This is not a complete solution. By itself, it doesn't work. You must also update Class.zip at C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033 – Adam Kane May 07 '12 at 17:57
  • 2
    This is Visual Studio 2010 specific. For 2012 it is located here: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class – James Hulse Nov 12 '12 at 22:34
  • any ideas how you could share this across machines, Darin? – DevDave Jul 31 '13 at 17:51
  • For VS 2013, we found that there is no longer a .zip file. Instead there is a folder. We changed the ItemTemplates version with no effect, even after restarting VS. When we changed the same file in the ItemTemplatesCache folder instead, VS 2013 picked up the change without restarting. – Prof Von Lemongargle Jun 30 '15 at 16:38
  • 1
    The path has changed on the newest version of Visual Studio. For 2019, the file path is : Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class – XavierAM Sep 17 '19 at 17:45
  • 1
    VS 2022 path: C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class – user3625699 Mar 16 '22 at 13:48
14

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.

Steve B
  • 36,818
  • 21
  • 101
  • 174
  • 1
    `CTRL +.` also works for displaying (intellisense*) smart tags, a nice way to do quick refactors – Chris S Jan 06 '12 at 09:57
  • This is not the intellisense, but the smart tag popout menu (don't know it's actual name). http://saraford.net/2005/05/02/did-you-know-use-shiftaltf10-to-invoke-smart-tags/ – Steve B Jan 06 '12 at 09:58
  • Sorry brain fart, I meant smart tags – Chris S Jan 06 '12 at 10:00
  • some sequels of the new year celebration ;) Anyways, the shortcut can be set up in the options, especially by setting the command `View.ShowSmartTag` in the `keyboard` section. Going further in the keyboard settings, you can assign the command `OtherContextMenus.SmartTag.GenerateClass` to spare one keyboard combination. – Steve B Jan 06 '12 at 10:06
  • This doesn't seem to be working in VS 2015. – James Ko Nov 20 '15 at 23:21
  • @JamesKo: in VS 2015, the shortcut is now CTRL+; – Steve B Mar 18 '16 at 16:20
  • what is `$endif$using System.Text;` line there? is it correct or there is a typo? i see that `using System.Text;` is not exists in my output file generated by VS – S.Serpooshan Dec 11 '18 at 12:07
2

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

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
2

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

See this link for a detailed HOW-TO.

ken2k
  • 48,145
  • 10
  • 116
  • 176