This is a good one!

Every one knows that when you add a new class to a project it is private by default, but still when you compile for the first time, the compilation fails and only then you remember to add the “public” before the class name. Tired of this? Here’s what you do in order to get Visual Studio to create the class as Public by default.

Go over to “Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033″, you will find a file called Class.zip, inside the .zip file open the file called Class.cs, the content of the file looks like this:

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

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

    All you need to do is add “Public” before the class name. The outcome should look like this:

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

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

One last thing you need to do is flush all the Templates Visual Studio is using, and make him reload them. The command for that is ( it takes a while so hold on): 

devenv /installvstemplates

And that’s it, no more private classes by default. Of course you can also add internal or whatever you want.

Enjoy

Amit

Tags :

3 Responses to “How to Get Visual Studio to Create New Classes Public by Default”


  1. denis.gz

    Said on May 15, 2008 :

    This is also the place to add a standard header to sources files, like copyright information and SVN $Id$ tag, so you don’t have to add manually – saves time.

  2. foobar

    Said on August 14, 2008 :

    > Every one knows that when you add a new class
    > to a project it is private by default

    No, not everyone. Some of us know classes by default are internal, and there’s a difference between private and internal.

  3. PHenry

    Said on January 28, 2009 :

    This was the inspiration for a template article I wrote tonight. I read this entry back in May and thought it would be cool to expand on it, awesome work guys! :>

    http://www.pchenry.com/Home/tabid/36/EntryID/71/Default.aspx

Post a Comment