Rhyous

February 17, 2010

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

Filed under: C# (C-Sharp),Visual Studio — J. Abram barneck @ 12:51 pm

Updated: 5/17/2010 using information aquired from here: http://www.thecodinghumanist.com/Content/HowToEditVSTemplates.aspx

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\
  3. Visual Studio 2008
    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\

  4. 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.

Advertisements

9 Comments »

  1. 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.

    Comment by rhyous — March 8, 2010 @ 10:55 am | Reply

  2. 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

    Comment by mkamoski1 — May 14, 2010 @ 5:31 am | Reply

    • Thanks for the Link.

      You might be right. My changes have disappeared again. I will try this.

      Comment by rhyous — May 17, 2010 @ 9:59 am | Reply

  3. 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.

    Comment by David L. Penton — August 19, 2010 @ 7:57 am | Reply

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

      Comment by rhyous — August 21, 2010 @ 9:13 pm | Reply

  4. 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

    Comment by Pierre — September 7, 2010 @ 2:14 am | Reply

    • 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.

      🙂

      Comment by rhyous — September 8, 2010 @ 4:28 pm | Reply

  5. […] How to modify the default new class template for C# in Visual Studio 2008 or 2010? […]

    Pingback by Add document header for files automatically in Visual Studio « Abhijit's World of .NET — December 4, 2010 @ 2:40 pm | Reply

  6. Hi,

    I’d like to do something like this, is it possible?

    public $if$ ($safeitemrootname$.EndsWith(“BLL”)) static $endif$class $safeitemrootname$

    Comment by Érick — October 2, 2012 @ 1:21 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: