How to modify the default new class template for C# in Visual Studio 2008 or 2010?

Updated: 5/17/2010 using information aquired from here: How To: Edit Visual Studio Templates

Ok, so I don’t like the way that the default new class template in Visual Studio 2008/2010 looks. I end up typing a lot of things over and over again.

Here is what it a new class looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyNameSpace
{
	class MyClass
	{
	}
}

Here is what I want it to look like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyNameSpace
{
	public class MyClass
	{
		#region Member Variables
		#endregion

		#region Constructors

		/// <summary>
		/// The default Constructor.
		/// </summary>
		public MyClass()
      		{
		}

		#endregion

		#region Properties
		#endregion

		#region Functions
		#endregion

		#region Enums
		#endregion
	}
}

So making this change is easy to do. All you have to do is edit a text file that is compressed.

Copy the zip file file located here to the desktop:
Visual Studio 2008

  • For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
  • For 32 bit: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Visual Studio 2010

  • For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
  • For 32 bit: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Extract the zip file.

Using a text editor, open the Class.cs file.

The file will have the following text:

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

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

Change it to have this text:

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

namespace $rootnamespace$
{
	public class $safeitemrootname$
	{
		#region Member Variables
		#endregion

		#region Constructors

		/// <summary>
		/// The default Constructor.
		/// </summary>
		public $safeitemrootname$()
		{
		}

		#endregion

		#region Properties
		#endregion

		#region Functions
		#endregion

		#region Enums
		#endregion
	}
}

Save the file.

Rebuild the zip file with the new Class.cs.  Be careful to build the zip file correctly.

Copy the new zip file back here and overwrite the existing one:
Visual Studio 2008

  • For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
  • For 32 bit: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Visual Studio 2010

  • For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
  • For 32 bit: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Now, you have to rebuild the template classes.  To do this:

  1. Open a command prompt as Administrator.
  2. Change to the appropriate directory:
    Visual Studio 2008
    64-bit

    cd C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\

    32-bit

    cd C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\

    Visual Studio 2010
    64-bit

    cd C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\

    32-bit

    cd C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\
  3. Run this command:
    devenv.exe /installvstemplates

Now any new class you create will have your new format.


Copyright ® Rhyous.com – Linking to this article is allowed without permission and as many as ten lines of this article can be used along with this link. Any other use of this article is allowed only by permission of Rhyous.com.

33 Comments

  1. ASP.NET MVC 3 consolidate namespace System.ComponentModel.DataAnnotations by default | Ziadie says:

    […] change a category template […]

  2. ufscar.Br says:

    ufscar.Br

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  3. indiw0rm.In says:

    indiw0rm.In

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  4. mrsec.com says:

    mrsec.com

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  5. Related Web Page

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  6. support.jaynescorp.com says:

    support.jaynescorp.com

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  7. models facebook facebook uk facebook social networking

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  8. More Material says:

    More Material

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  9. web-house.jsy.ne.jp says:

    web-house.jsy.ne.jp

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  10. click the following website

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  11. party-wagon.com

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? | Rhyous

  12. Muuski says:

    The file path shown in this tutorial didn't work for me. I am using Windows 7 64 bit and I actually found the class.cs file in;

    C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\VCSExpress\ItemTemplates\1033\Class.zip

    note the VCSExpress folder after IDE as opposed to

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

    which only had some .zips concerning ADO .Net

  13. [...] of the class file. This is mundane and I forget all the time. After a quick Google search, this website showed me how to automate adding a copyright to all new C# class files in VS 2008 and [...]

  14. Thomas says:

    Nice man, good tutorial. Short and easy to understand. Thumbs up!

  15. Visual Studio Automatic Copyright | The Techie Bits says:

    [...] of the class file. This is mundane and I forget all the time. After a quick Google search, this website showed me how to automate adding a copyright to all new C# class files in VS 2008 and [...]

  16. ASP.NET MVC 3 include namespace System.ComponentModel.DataAnnotations by default | PHP Developer Resource says:

    [...] change the class template (or export a second class template) [...]

  17. Cache says:

    You can directly change in the cache and you do not need to do devenv.exe /installvstemplates.

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

  18. Pierre says:

    Hi,

    This is hacking into MVS system where there is no need.

    Just create a class template ( File-->Export Template...) and reuse it whenever you add a class to your project.

    You can find more details there http://msdn.microsoft.com/en-us/library/tsyyf0yh(v=VS.80).aspx

    Cheers

    Pierre

    • rhyous says:

      Maybe. But then I have the option to add a Class or a MyClassTemplate, and then I will just annoy myself every time I choose Class instead of MyClassTemplate.

      Also, I can right-click a project and choose Add | Class and the item list comes up with Class selected and ready for a name. With your method, I have to right-click a project and and choose Add | New Item. Then look through a large item list for MyClassTemplate and highlight it myself. As dumb as it sounds, your way takes more clicks.

      🙂

  19. Hi there,

    Those regions are considered as a code-smell. If you have so much in your class that you need regions, then you need to refactor into smaller, more concise parts.

    • rhyous says:

      I think I disagree with you. However, your code nose may be a better sniffer than mine.

    • Adam Ralph says:

      Whilst this is not relevant to the post topic itself. I agree - IMHO, regions should not be used.

    • Mike says:

      I say nonsense. I like regions and grouping methods, properties, constructors has nothing whatsoever with needing to refactor code. They provide a human friendly way to organize code and also a mechanism for colapsing/ expanding sections of code. You, rhyous, make sense to me. You others, don't use 'em. They don't cost you anythign and if you scatter your code around - it's your code until someone like me is your boss or lead and guess what - you will use them.

      • Adam Ralph says:

        @Mike - I guess it comes down to whether you need to use regions or not. I tend to follow domain driven design and the SRP principle quite tightly, which means the code for most of my classes fit into one screen fully expanded, i.e. I have no *need* to use regions. For me, when someone *needs* to use regions because their classes are so big, that in itself is a code smell.

        • Rhyous says:

          Interesting how you assume having regions indicates a large class.

          My classes may have only a couple of properties.

          Also, I used MVVM a lot, and so because of the need to call INotifyPropertyChanged, the properties are bigger.

                  public int MyProperty
                  {
                      get { return _MyProperty; }
                      set
                      {
                          _MyProperty = value;
                          NotifyPropertyChanged("MyProperty");
                      }
                  } private int _MyProperty;
          

          So 9 lines for a MVVM property, it only takes four properties to start making a class to big to be on one screen.

          I get what you are saying, that class should be small, should follow well designed principles, and I am all about that. However, having or not having regions has nothing to do with what you are talking about.

      • Steven says:

        *shiver* it may help the writer to organise the code, but it only hinders the reader by *hiding the very code they're trying to understand*! Also: I know what a ctor/property/field looks like thanks very much.

        • Rhyous says:

          Maybe that is true for one random file...but you are thinking about looking at one file. By having such a format, when you project is now 100 class files in multiple dlls (projects), and by using this method all classes are formatted the same way, every class with the same layout, constructors and properties in the same place, it actually increases the readability of the project. Be aware, others have hated them at first, but after using a consistent region theme, they changed their minds.

          • Adam Ralph says:

            If the goal is consistent ordering by member type and access then it's best to use an analysis tool like StyleCop which actually enforces this.

        • Richard says:

          I use regions - I like them. Whilst they can hide code (as can collapsing a method - without regions! They can also be commented by placing text after the #region. I have this a mandatory on my Region Snippet - it makes you type a comment into the region to say what's hidden and automatically places the same comment on the #endregion. WinForms are also classes, and difficult to make small if the form has any menus/toolbars or other child controls - even if you decide to go for the nightmare call an external class in each and every form callback (ahghghgh) - #region Menu Callbacks....#endreegion Menu Callbacks; etc.

          As with anything, it can be overused, misused and used badly - doesn't mean it shouldn't be used though. If you work for a large company and have to mmaintain code written by someone else, standards are important to help readability and consistancy - regions can be used under the same policies.

  20. mkamoski1 says:

    Rhy --

    Regarding this...

    >>> Today this change was gone

    ...did you remember to rebuild the template cache?

    I think that may be required.

    See this link...

    http://www.thecodinghumanist.com/Content/HowToEditVSTemplates.aspx

    ...for details.

    HTH.

    Thank you.

    -- Mark Kamoski

  21. rhyous says:

    Today this change was gone. I am trying to remember the changes I performed. I know I installed Expression Blend, and maybe that overwrote my class.zip, but I can't be sure.

Leave a Reply

How to post code in comments?