views:

62

answers:

4

When I add a new class to a project, the class modifier/access specifier for the newly created class in Visual Studio is ommitted, thereby making it internal.

Is there a way I can specify in Visual Studio settings that whenever I ask for a new class to be created, please make the class public?

I looked in the Tools->Options menu but couldn't find anything. I am guessing it could be in the Templates folder in the My Documents\Visual Studio xxxx folder.

+3  A: 

Yes, you can change the file templates (or simply provide secondary file templates), like so. However, before you do that, I would ask myself: does it really take that much effort to add the desired accessibility? I tend to err on the side of the pragmatic when it comes to this... if I wanted significantly different templates, I might do it. Otherwise... meh.

Marc Gravell
Thanks very much.
Water Cooler v2
+2  A: 

You would have to create your own template to do this, or use code snippets, or perhaps a tool like ReSharper, which has its own snippet mechanism.

However, this is likely to be a bad idea. You will be making things public that do not need to be public. You're better off starting everything as internal, and making them public only as required.

John Saunders
+2  A: 

A quick search turned up this:

http://stackoverflow.com/questions/873411/prevent-visual-studio-from-adding-default-references-and-usings-for-new-classes

The answer demonstrates that you can change the default class template.

Hope that helps.

Kieren Johnstone
+2  A: 

If you omit the modifier on a class it is either:

  • internal (non-nested classes)
  • private (on nested classes)

I just thought I'd throw that out there (that it's not ALWAYS internal if omitted).


But, if you really want to change templates you'll want to generally go to the installation folder (I'll use the default folders):

*C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE*

In there, you will find the various template folders. You're looking for the following (if you dig deeper): C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Just alter the Class.cs file in there. Oh, and in case you mess it up here's the original version:

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

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

You're desired version:

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

namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
    }
}
myermian
The only time something is 'public' without being marked 'public', is `get` and `set` constructs for properties, when the property itself is `public` :) From Jon Skeet's book :)
Kieren Johnstone